原标题:Python-12306模拟自动购票!快过年了,学起来啊!
from time import sleep
from selenium import webdriver
def login():
my_user = '我的用户名'
my_passwords = '我的密码'
script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});'
bro.execute_script(script)
sleep(0.5)
# 点击一下使用账号密码登录
bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
bro.find_element_by_id('J-userName').send_keys(my_user)
sleep(1)
bro.find_element_by_id('J-password').send_keys(my_passwords)
print(u"手动验证---")
while True:
if bro.current_url != url:
print("登录成功---")
break
def main():
login()
start = '%u5357%u660C%2CNCG'#出发点
end = '%u5409%u5B89%2CVAG'#到达点
time = '-12-01'
id = '学生'
order = 0
#order即列车票的顺序,0则为从上至下依次点击,选择有票的列车购买
#若是不为0,则按输入的购买
sleep(3)
# print(bro.current_url)
script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});'
bro.execute_script(script)
bro.find_element_by_link_text('确定').click()
bro.find_element_by_link_text("车票").click()
bro.find_element_by_link_text("单程").click()
sleep(3)
url = bro.current_url
bro.add_cookie({"name":"_jc_save_fromStation","value":start})
bro.add_cookie({"name":"_jc_save_toStation","value":end})
bro.add_cookie({"name":"_jc_save_fromDate","value":time})
bro.refresh()
if id == '学生':
bro.find_element_by_id('sf2').click()
else:
bro.find_element_by_id('sf1').click()
sleep(1)
if order == 0:
while True:
if url != bro.current_url:
break
print("正在查询,客官莫急---")
bro.find_element_by_id('query_ticket').click()
try:
sleep(1)
for i in bro.find_elements_by_class_name('btn72'):
i.click()
sleep(2)
except:
print("还未预定,请等待---")
continue
else:
while True:
if url != bro.current_url:
break
print("正在查询,客官莫急---")
bro.find_element_by_id('query_ticket').click()
try:
sleep(1)
bro.find_elements_by_class_name('btn72')[order-1].click()
sleep(2)
except:
print("还未预定,请等待---")
continue
sleep(3)
print("进入预定---")
print("正在选择乘客---")
bro.find_elements_by_class_name('check')[2].click()
#默认只有自己
bro.find_element_by_link_text('确认').click()
print("乘客选择完成---")
bro.find_element_by_xpath('//*[@id="seatType_1"]//option[@value="1"]').click()
print("提交订单---")
bro.find_element_by_link_text("提交订单").click()
sleep(1)
bro.find_element_by_link_text('确认').click()
print("订单已提交---")
print("请客官前往支付---")
if __name__=='__main__':
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
bro = webdriver.Chrome(options=option)
bro.maximize_window() # 浏览器全屏显示
url = '/otn/resources/login.html'
bro.get('/otn/resources/login.html')
责任编辑: