python+selenium-12306實戰解決登錄滑塊問題
- 寫在前面
- 遇到的問題
- 解決辦法
- 12306登錄程序
- 如何使用開發者模式
- 參考
寫在前面
嘗試自動化測驗或初級爬蟲(登錄模塊)以及滑塊驗證可參考本篇文章,
使用網站:chrome(39版)
遇到的問題
滑塊驗證:賬號密碼輸入后會讓進行滑塊驗證,即使拖動成功也會讓重繪再試一次,即使再拖動仍舊如此,問題其實出在chromedriver,網站會檢測到是自動測驗工具從而阻止你的登錄,
解決辦法
使用低版本的chrome以及對應的chromedriver,具體版本對應csdn上有可以搜到(文末也有注釋以及chromedriver各版本下載地址),至此可解決12306的滑塊問題,但有一個問題該版本不支持購買服務,只能作為練習了,
對于其他網站,如:某淘,本人在去年想攻克未遂,今年又遇到相同的問題找到一篇解決方法,適用性很高,大家可以嘗試下,注意使用低版本的chrome以及chromedriver(39可以),才能find 到$cdc_lasutopfhvcZLmcfl,文章鏈接,
我使用的chrome是39版本,下載地址:http://www.121down.com/soft/softview-103980.html#downaddress
① 記得設定禁止自動更新:https://blog.csdn.net/weixin_41990913/article/details/90912790
②還有環境變數配置,由于老版本chrome不一定裝在C盤,要根據具體位置加到用戶變數PATH中,chromedriver.exe跟chrome.exe放在同一個檔案夾中,
12306登錄程序
注釋比較詳細,不再做過多說明
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
#-------------------------------------初始化--------------------------------------------
#僅用chrome39版本可躲過selenium檢測
option = webdriver.ChromeOptions()
#開發者模式的開關,設定一下,打開瀏覽器就不會識別為自動化測驗工具
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.binary_location = "D:\\Google Chrome 32位 v39.0.2171.99\\App\\Google Chrome\\chrome.exe" #自己目錄chrome位置
driver = webdriver.Chrome(chrome_options=option, executable_path="D:\\Google Chrome 32位 v39.0.2171.99\\App\\Google Chrome\\chromedriver")#自己目錄chromedriver位置
#------------------------------------打開12306網頁-------------------------------------------
driver.maximize_window()
driver.implicitly_wait(10)
url = 'https://kyfw.12306.cn/otn/leftTicket/init'
driver.get(url)
#-------------------------------------登錄---------------------------------------------
ActionChains(driver).move_by_offset(1000, 560).click().perform() # 點掉提示,滑鼠左鍵點擊, 1000為x坐標, 500為y坐標,可用微信截圖工具得出坐標
driver.find_element_by_id("login_user").click()
driver.find_element_by_xpath("//ul[@class='login-hd']/li[2]/a").click() #注意相對路徑也要每層都寫,層層遞進
driver.find_element_by_id('J-userName').send_keys("")
driver.find_element_by_id('J-password').send_keys("")#補充自己的賬號密碼
driver.find_element_by_xpath("//div[@class='login-btn']/a").click()#登錄
sleep(2)
#-----------------------------------滑塊驗證--------------------------------------------
#拖動滑塊
dragger = driver.find_element_by_id("nc_1_n1z")
action = ActionChains(driver)
action.click_and_hold(dragger).perform()# 點擊并拖住滑塊
action.drag_and_drop_by_offset(dragger,300,0).perform() #拖動等長距離滑塊,開發者模式可看到
sleep(2)
driver.find_element_by_xpath("//div[@class='modal-ft']/a").click()# 點掉提示
如何使用開發者模式
- 首先按F12
- 如何快速找到按鈕對應的html代碼位置:點擊開發工具左上角,如圖所示

- 再點擊你想要找的按鈕,比如登錄,右側開發工具會自動顯示對應的html代碼
參考
某寶: https://blog.csdn.net/taojian_/article/details/97758632?spm=1001.2014.3001.5501
版本對應: https://blog.csdn.net/Yunwubanjian/article/details/86539432
chromedriver: http://chromedriver.storage.googleapis.com/index.html
12306實戰: http://www.uml.org.cn/Test/201808024.asp
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312100.html
標籤:其他
上一篇:性能測驗,你需要了解這款工具
