from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
# 创建浏览器对象
options = webdriver.EdgeOptions()
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
print("浏览器驱动初始化")
#browser = webdriver.Chrome(options=options)
browser = webdriver.Edge(options=options)
# 创建ActionChains对象
actions = ActionChains(browser)
# 访问首页
browser.get("https://www.12306.cn/index/")
time.sleep(1)
cs = browser.execute_script("return document.cookie;")
print(cs)
#browser.quit
#登录
print("准备登陆")
loginButton = browser.find_element(By.ID,"J-btn-login")
actions.move_to_element(loginButton).click().perform()
loginUserNameInput = browser.find_element(By.ID,"J-userName")
actions.move_to_element(loginUserNameInput).click().perform()
# TODO 从控制台读取
loginUserNameInput.send_keys("188****721")
loginPassWordInput = browser.find_element(By.ID,"J-password")
actions.move_to_element(loginPassWordInput).click().perform()
loginPassWordInput.send_keys("xxx")
loginSubmitButton = browser.find_element(By.ID,"J-login")
actions.move_to_element(loginSubmitButton).click().perform()
print("当前账号"+loginUserNameInput.get_attribute('value')+"密码"+loginPassWordInput.get_attribute('value'))
time.sleep(1)
loginIdCardInput = browser.find_element(By.ID,"id_card")
actions.move_to_element(loginIdCardInput).click().perform()
loginIdCardInput.send_keys("2333")
time.sleep(1)
print("获取手机验证码")
loginVerificationButton = browser.find_element(By.ID,"verification_code")
actions.move_to_element(loginVerificationButton).click().perform()
loginVerificationButton.send_keys("verification_code")
# TODO 等待接收验证码自动填入框架点击提交
print("等待手机验证码返回中")
time.sleep(30)
print("执行登录中......")
loginSureClicVerificatiokButton = browser.find_element(By.ID,"sureClick")
actions.move_to_element(loginSureClicVerificatiokButton).click().perform()
print("登录成功啦啦啦,休眠5S,准备跳转首页")
time.sleep(5)
# 登录成功:点击首页
cs = browser.execute_script("return document.cookie;")
print(cs)
redirectIndexLi = browser.find_element(By.ID,"J-index")
actions.move_to_element(redirectIndexLi).click().perform()
print("访问首页中")
# 模拟查询车站
# 模拟操作输入框选择车站
fromStationTextInput = browser.find_element(By.ID,"fromStationText")
# 鼠标移动到出发站输入框上方
actions.move_to_element(fromStationTextInput).click().perform()
fromStationTextInput.clear
fromStationTextInput.send_keys("shanghai")
fromStationTextInput.send_keys(Keys.ENTER)
print("用户选择的出发地"+fromStationTextInput.get_attribute('value'))
toStationTextInput = browser.find_element(By.ID,"toStationText")
actions.move_to_element(toStationTextInput).click().perform()
toStationTextInput.clear
toStationTextInput.send_keys("beijing")
toStationTextInput.send_keys(Keys.ENTER)
print("用户选择的到点地"+fromStationTextInput.get_attribute('value'))
# 查询时间
toTrainDateInput = browser.find_element(By.ID,"train_date")
actions.move_to_element(toTrainDateInput).click().perform()
toTrainDateInput.send_keys(Keys.CONTROL,'a')
toTrainDateInput.send_keys("2023-08-30")
browser.execute_script("document.querySelector('.cal-wrap').style.display='none';")
# time.sleep(1)
time.sleep(5)
print("用户选择的出发时间"+toTrainDateInput.get_attribute('value'))
#searchTickeyButton.click
# 模拟点击查询跳转查询页面
searchTickeyButton = browser.find_element(By.ID,"search_one")
print(searchTickeyButton.id)
searchTickeyButton.click
actions.move_to_element(searchTickeyButton).click().perform()
# 切换查询页面,不断的查询
windows = browser.window_handles
browser.switch_to.window(windows[-1])
time.sleep(1)
queryTickeyButton = browser.find_element(By.ID,"query_ticket")
#TODO 构建查询GET请求查票
time.sleep(1)
print("开始查询车票")
actions.move_to_element(queryTickeyButton).click().perform()
cs = browser.execute_script("return document.cookie;")
print(cs)
print("开始提交订单...")
time.sleep(10)
cs = browser.execute_script("return document.cookie;")
print(cs)
while 1==1:
time.sleep(5)
cs = browser.execute_script("return document.cookie;")
print(cs)
Python+Selenium实现12306自动化刷票脚本
于 2023-09-19 00:26:19 首次发布