import time标签:selenium,driver,find,12306,sleep,模块,date,element,click From: https://www.cnblogs.com/charleyhoot/p/16644673.html
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
js = 'window.navigator.webdriver '
js1 = 'return window.navigator.webdriver'
driver = Chrome(options=chrome_options)
driver.execute_script(js)
driver.get('https://www.12306.cn/index/')
user_login = driver.find_element(By.ID, 'J-btn-login')
user_login.click()
user_input = driver.find_element(By.ID, 'J-userName')
user_input.send_keys('User-ID')
time.sleep(2)
pwd_input = driver.find_element(By.ID, 'J-password')
pwd_input.send_keys('Password')
time.sleep(2)
login_button = driver.find_element(By.ID, 'J-login')
login_button.click()
time.sleep(2)
# Verifying Code Test
scroll_button = driver.find_element(By.ID, 'nc_1_n1z')
Action = ActionChains(driver=driver)
Action.click_and_hold(scroll_button)
for i in range(5):
Action.move_by_offset(80, 0).perform()
time.sleep(0.1)
Action.release()
result = driver.execute_script(js1)
print(result)
time.sleep(10)
# Enough Time To Load The Confirm Button
# Input Data And Chose Station
confirm_button = driver.find_element(By.XPATH, '//*[@role="alertdialog"]/div[3]/a')
confirm_button.click()
first_page_button = driver.find_element(By.XPATH, '//*[@id="J-index"]/a')
first_page_button.click()
station_input_from = driver.find_element(By.XPATH, '//*[@id="fromStationText"]')
station_input_from.click()
time.sleep(1)
station_input_from.send_keys('station from')
station_choose_from = driver.find_element(By.XPATH, '//*[@id="citem_0"]')
station_choose_from.click()
station_input_to = driver.find_element(By.XPATH, '//*[@id="toStationText"]')
station_input_to.click()
time.sleep(1)
station_input_to.send_keys('station to')
station_choose_to = driver.find_element(By.XPATH, '//*[@id="citem_0"]')
station_choose_to.click()
date_year_month = input('年月:')
date_day = input('日期:')
date_content = date_year_month+'-'+'0'+date_day
date_choose = driver.find_element(By.XPATH, '//*[@id="train_date"]')
date_choose.clear()
time.sleep(1)
date_choose.click()
time.sleep(1)
date_choose.send_keys(date_content)
refresh_click = driver.find_element(By.XPATH, '//*[@id="train_date"]')
refresh_click.click()
time.sleep(2)
date_xpath = '//*[@id="toolbar_Div"]/div[10]/div[1]/div[2]/'+'div['+date_day+']'
date_confirm_click = driver.find_element(By.XPATH, date_xpath)
date_confirm_click.click()
time.sleep(1)
search_button = driver.find_element(By.XPATH, '//*[@id="search_one"]')
search_button.click()
time.sleep(10)
# Get The Train Station Information
train_number = driver.find_element(By.XPATH, '//*[@id="t-list"]/table/tbody/tr')
print(train_number)
result = driver.execute_script(js1)
print(result)
time.sleep(3)