1 import time
2 from selenium.webdriver import Chrome
3 from selenium.webdriver.chrome.options import Options
4 from selenium.webdriver.common.by import By
5 from selenium.webdriver.common.action_chains import ActionChains
6
7 opt = Options()
8 # 防止被识别
9 opt.add_argument('--disable-blink-features=AutomationControlled')
10 # 不自动关闭浏览器
11 opt.add_experimental_option("detach", True)
12
13 web = Chrome(options=opt)
14 web.get('https://kyfw.12306.cn/otn/resources/login.html')
15
16 # 输入账号密码登陆
17 web.find_element(By.XPATH, '//*[@id="J-userName"]').send_keys('176*****409')
18 web.find_element(By.XPATH, '//*[@id="J-password"]').send_keys('********')
19 web.find_element(By.XPATH, '//*[@id="J-login"]').click()
20 time.sleep(1)
21 # 拖拽验证
22 btn = web.find_element(By.XPATH, '//*[@id="nc_1__scale_text"]/span')
23 ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()