"""
在12306首頁進(jìn)行出發(fā)地掰烟、目的和出發(fā)時(shí)間的操作
文本框先點(diǎn)擊
后輸入城市名稱
再選擇輸入的城市
"""
# 導(dǎo)入Selenium的Webdiver包
from selenium import webdriver
#導(dǎo)入鼠標(biāo)事件的包
from selenium.webdriver.common.action_chains import ActionChains
#導(dǎo)入時(shí)間
from time import sleep
# 定義瀏覽器對象
driver=webdriver.Firefox()
# 瀏覽器打開12306首頁
driver.get("https://www.12306.cn/index/")
sleep(3)
# 清除文本內(nèi)容甲棍,并定位元素
driver.find_element_by_id("fromStationText").click()
sleep(2)
driver.find_element_by_id("fromStationText").send_keys("西安")
sleep(2)
# 調(diào)用actionChains相關(guān)內(nèi)容熊户,實(shí)現(xiàn)鼠標(biāo)移動到指定的元素
ActionChains(driver).click(driver.find_element_by_id("citem_1")).perform()
sleep(3)
# 清除文本內(nèi)容,并定位元素
driver.find_element_by_id("toStationText").click()
sleep(2)
driver.find_element_by_id("toStationText").send_keys("北京西")
sleep(2)
# 鼠標(biāo)點(diǎn)擊到指定元素
ActionChains(driver).click(driver.find_element_by_id("citem_0")).perform()
sleep(3)
# 定位出發(fā)日期元素罐盔,并移除當(dāng)前時(shí)間
js = "document.getElementById('train_date').removeAttribute('readonly');"
driver.execute_script(js)
js_set="document.getElementById('train_date').value='2020-11-14';"
driver.execute_script(js_set)
sleep(2)
driver.find_element_by_link_text("查? ? 詢")