现场环境:python 3.7.7,selenium3.14
- 代码在原先基础上加了滑动验证,在第一版(2020年底)12306网站没了图片验证,难度降低了不少嘿嘿嘿。在跳转买票的代码前做过对时间转整数的判定,效果很凄惨,还是不能做到“抢”票的概念,哎。有大佬有很好用的抢票的,就安利我一下呗
遇到的问题:
- 解救滑块验证的英雄:script = “Object.defineProperty(navigator, ‘webdriver’, {get:()=>undefined,});”
- 在选择出发/到达地 用的拼音缩写:在去除掉表单的只读貌似不管用。
- 谷歌驱动:按照收集的方案好像都不适用,没办法时就重装了谷歌默认安装,把驱动放到谷歌文件夹里它就好了。
- 在装selenium库时报错本地有不兼容的库,按照报错提示去重装对应的版本。
- 在选票和乘车人时,好像可以用文本去匹配(我没写)
- 了解一下:selenium库,time库,id选择器,(多线程)
代码如下
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Chrome, ActionChains
# #防止网站禁止selenium
# script = "Object.defineProperty(navigator, 'webdriver', {get:()=>undefined,});"
def login():
from_name = "HZ" #出发站拼音简写
to_name = "XY" #到达站拼音简写
to_date = "2022-03-29"
#set_name = '硬座'
username = '账号'
password = '密码'
#options=webdriver.ChromeOptions()
# 忽略无用的日志
# options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
driver = webdriver.Chrome() #chrome_options=options
driver.maximize_window()
driver.get("https://kyfw.12306.cn/otn/resources/login.html")
script = "Object.defineProperty(navigator, 'webdriver', {get:()=>undefined,});" #防止浏览器禁止Selenium
driver.execute_script(script)
# time.sleep(1)
#driver.find_element_by_xpath("/html/body/div[2]/url/li[2]/a").click()
time.sleep(2)
# // *[ @ id = "s-top-loginbtn"]
#driver.find_element_by_id("J-userName").send_keys(username)
#driver.find_elements_by_id("J-password").send_keys(username)
driver.find_element(By.ID, 'J-userName').send_keys(username)
time.sleep(1)
driver.find_element(By.ID, 'J-password').send_keys(password)
time.sleep(0.7)
driver.find_element(By.ID, 'J-login').click()
time.sleep(3)
#滑动验证
try:
span = driver.find_element(By.ID, 'nc_1_n1z')
action = ActionChains(driver)
action.click_and_hold(span)
time.sleep(4)
action.move_by_offset(300, 0).perform()
except Exception as a1:
print(a1)
finally:
time.sleep(3) #改小了和没有就报错
#driver.find_element(By.CLASS_NAME, 'ok').click()
driver.find_element(By.CSS_SELECTOR, 'a.ok').click() #点击‘确定’ 消失弹窗
time.sleep(1)
driver.find_element(By.CSS_SELECTOR, 'a[name="g_href"]').click() #点击 ‘首页’
time.sleep(1)
#driver.execute_script('document.getElementById("fromStationText").removeAttribute("readOnly");') #除去表单的只读
from_input = driver.find_element(By.ID, "fromStationText")
from_input.click()
from_input.clear()
time.sleep(1)
from_input.send_keys(from_name)
time.sleep(2)
from_input.send_keys(Keys.ENTER)
time.sleep(1)
from_input.send_keys(Keys.TAB)
time.sleep(2)
to_input = driver.find_element(By.ID, 'toStationText') # 编写到达地
to_input.click()
to_input.clear()
time.sleep(3)
to_input.send_keys(to_name)
time.sleep(2)
to_input.send_keys(Keys.ENTER)
time.sleep(2)
todo_date = driver.find_element(By.ID, 'train_date')
todo_date.clear()
todo_date.click()
time.sleep(1)
todo_date.send_keys(to_date)
time.sleep(1)
driver.find_element(By.CSS_SELECTOR, 'i.icon').click()
time.sleep(5)
driver.find_element(By.ID, 'search_one').click()
n = driver.window_handles #页面句柄
driver.switch_to.window(n[-1])
time.sleep(0.6)
driver.find_elements(By.CLASS_NAME, "btn72")[1].click()
time.sleep(0.3)
driver.find_element(By.ID, 'normalPassenger_0').click() #0 是第一个乘车人
time.sleep(0.2)
driver.find_element(By.ID, 'seatType_1').click()
time.sleep(0.2)
driver.find_element(By.ID, 'submitOrder_id').click() #提交订单
time.sleep(0.2)
#driver.find_element(By.ID, 'qr_submit_id').click() #最后抢票弹窗的确认
time.sleep(500)
driver.close()
if __name__ == '__main__':
login()
尝试过得判断时间,不过没啥效果0.0
import datetime
M = 29
i = 59
y = 1
while True:
time_local = datetime.datetime.today().strftime("%H-%M-%S-%f")
list_test = time_local.split('-')
print(list_test)
if int(list_test[1]) == M and int(list_test[2]) == i and int(list_test[3]) > y:
break
else:
print(list_test)
你可以在这两处time.sleep( )里的值改小一点,然后观察一下。我用了很多的 time.sleep() 是真的怕了