1. 程式人生 > 其它 >Python+selenium實現自動登入12306

Python+selenium實現自動登入12306

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 from time import sleep
 4 from selenium.webdriver import Chrome
 5 from selenium.webdriver.common.action_chains import ActionChains
 6 
 7 options = webdriver.ChromeOptions()
 8 #無痕模式
 9 options.add_argument('--incognito
') 10 options.add_experimental_option('excludeSwitches',['enable-automation']) 11 options.add_experimental_option("detach",True) 12 #躲過瀏覽器控制元件的檢測 13 options.add_argument('--disable-blink-features=AutomationControlled') 14 #啟動chrome瀏覽器無痕模式 15 driver=webdriver.Chrome(chrome_options=options) 16 #找到登入按鈕 17
driver.get("https://www.12306.cn/index/") 18 driver.find_element(By.XPATH,'//*[@id="J-btn-login"]').click() 19 #輸入賬號和密碼點選立即登入 20 driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/input").send_keys("000000000") 21 driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div[2]/input
").send_keys("000000000") 22 driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div[4]/a").click() 23 sleep(2) 24 #找到滑塊並拖拽 25 slider=driver.find_element(By.XPATH,"/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/span") 26 ActionChains(driver).drag_and_drop_by_offset(slider,340,0).perform()