Python+Selenium实现12306自动化刷票脚本

文章介绍了如何使用Selenium库中的WebDriver和EdgeOptions在Python中自动化登录12306网站,包括处理cookies、输入用户名、密码、验证码以及查询车票的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值