js日期框代码2

# 在f12控制台中的console中可以进行js语句的输入

import time
from datetime import datetime,timedelta
from pykeyboard import PyKeyboard
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 建立driver会话打开12306网页
driver=webdriver.Chrome()
driver.get("https://www.12306.cn/index/")
# 选择出发地
loc=(By.XPATH,'//input[@id="fromStationText"]')
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc))
time.sleep(2)
fromStation='杭州'
fs='HZH'
# 选择出发地的js语句
fromStation_js="""
document.getElementById('fromStation').value=arguments[0];
var from=document.getElementById('fromStationText');
from.className="input inp-txt_select";
from.value=arguments[1];
"""
driver.execute_script(fromStation_js,fs,fromStation)
time.sleep(1)
# 选择目的地
loc=(By.XPATH,'//input[@id="toStationText"]')
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc))
time.sleep(1)
toStation="天水"
ts="TSJ"
# 选择目的地的js语句
toStation_js="""
document.getElementById("toStation").value=arguments[0];
var to=document.getElementById("toStationText");
to.className="input inp-txt_select";
to.value=arguments[1];
"""
driver.execute_script(toStation_js,ts,toStation)
time.sleep(1)

# 选择出发日期
# 当前日期+10天,转成字符串格式
# date = str(datetime.now().date()+timedelta(days=10))
date="2022-01-23"
# 使用js修改日期框为可输入状态,传入日期
date_js = """
var date = document.getElementById('train_date');
date.value = arguments[0];
"""

driver.execute_script(date_js,date)
time.sleep(3)
# WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"search_one")))
# 点击查询按钮
loc=(By.XPATH,'//a[@id="search_one"]')
driver.find_element(*loc).click()

# 切换到新窗口中
all_handles=driver.window_handles
driver.switch_to.window(all_handles[-1])
time.sleep(3)
# 处理弹出框中
k=PyKeyboard()
k.press_key(k.return_key)
time.sleep(3)
driver.quit()
上一篇:c – 将PXCImage转换为OpenCV Mat


下一篇:双十一刚过,写了一个selenium爬取淘宝商品信息的爬虫