selenium+python实现12306自动化抢火车票(二)

往期回顾:

selenium+python实现12306自动化抢火车票(一)

1、根据乘车人姓名匹配,支持1人或多人选择

定位出所有乘车人的元素集,根据姓名集合去元素集里循环迭代匹配,匹配上了操作选中

go 复制代码
ele_all=driver.find_elements(By.XPATH, "//*[@id='normal_passenger_id']/li")#定位所有乘车人for i in name_list:    for j in range(len(ele_all)):        if i==ele_all[j].text:            driver.find_element(By.ID, "normalPassenger_{0}".format(j)).click()#选择乘车人            print("选择乘车人【{0}】".format(i))

2、车次未开放或已售完,持续查询

车次售完了,或还未开售,这时,预定按钮是灰色不可点击的,这里要做特殊处理,可点击的预定td标签下有a标签,不可点击预定td标签下没有a标签,这时可根据是否有a标签判断预定按钮是否可点击,可点击继续后续流程,不可点击,继续进行查询操作,持续循环,直至有票了或车票开售

sql 复制代码
bool = Truewhile bool:    driver.find_element(By.ID, "query_ticket").click() #点查询    print("{0}------{1},日期:【{2}】,车次:【{3}】,查询中~".format(from_city,to_city,date,train_no))    ele=driver.find_elements(By.XPATH, "//div[@class='train']") #列表所有车次    for i in range(len(ele)):        a=True        try:            ele1=driver.find_element(By.XPATH, "//div[@id='train_num_{0}']/../../td[13]/a".format(i))        except:            a=False            print("{0}------{1},日期:{2},车次:{3},车次不存在/还未开售/已售完~".format(from_city, to_city, date, train_no))        if train_no in ele[i].text and a:            print("{0}------{1},日期:【{2}】,车次:【{3}】,已查到车次,且有可买的车票~".format(from_city, to_city, date,train_no))            ele1.click() #点预定            print("{0}------{1},日期:【{2}】,车次:【{3}】,预定中~".format(from_city, to_city, date,train_no))            try:                driver.find_element(By.ID, "login")                driver.find_element(By.ID, "login_close").click()                ele1.click() #点预定                print("{0}------{1},日期:【{2}】,车次:【{3}】,再次预定中~".format(from_city, to_city, date, train_no))            except:                pass            bool=False            break

3、根据不同车次座位类别,选对应的座位席位

go 复制代码
ticket_info = driver.find_elements(By.XPATH, "//*[@id='check_ticketInfo_id']/tr") #确认页,乘车人车票信息for i in range(len(ticket_info)):    ticket_info_seat=driver.find_elements(By.XPATH, "//*[@id='check_ticketInfo_id']/tr[{0}+1]/td[2]".format(i)) #确认页,乘车人座位类别    if "一等座"==ticket_info_seat[0].text and list[i] in ("1A","2A","1C","2C","1D","2D","1F","2F"):        driver.find_element(By.ID, "{0}".format(list[i])).click()    elif "二等座"==ticket_info_seat[0].text and list[i] in ("1A","2A","1B","2B","1C","2C","1D","2D","1F","2F"):        driver.find_element(By.ID, "{0}".format(list[i])).click()    elif "商务座" == ticket_info_seat[0].text and list[i] in ("1A","2A","1C","2C","1F","2F"):        driver.find_element(By.ID, "{0}".format(list[i])).click()

代码如下

👇👇👇

相关推荐
FreakStudio2 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663673 小时前
使用 Python 从零创建 Word 文档
python
Csvn8 小时前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽9 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户5569188175311 小时前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
兵慌码乱1 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei1 天前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
aqi001 天前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn1 天前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python