Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
好久沒有學(xué)python了,反正各種理由吧(懶惰總會有千千萬萬的理由),最近網(wǎng)上學(xué)習(xí)了一下selenium,實現(xiàn)了一個簡單的自動登錄網(wǎng)頁,具體如下。
1.安裝selenium:
如果你已經(jīng)安裝好anaconda3,直接在windows的dos窗口輸入命令安裝selenium:
python -m pip install --upgrade pip
查看版本pip show selenium
2.接著去http://chromedriver.storage.googleapis.com/index.html下載chromedriver.exe(根據(jù)chrome的版本下載對應(yīng)的)
3.將下載好的chromedriver.exe解壓后放到指定目錄
4.安裝tesseract-ocr.exe 配置環(huán)境變量
5.安裝pytesseract : pip install pytesseract
6.python腳本
思路:6.1登錄頁面按F12檢查元素,獲取用戶名 密碼 驗證碼 驗證碼圖片的元素id
6.2.調(diào)用chromedriver
6.3.截取驗證碼圖片的位置
6.4.pytesseract識別圖片中字符,最后驗證碼識別為空!?。???這個待解決
6.5.腳本如下:
from selenium import webdriver from PIL import Image import pytesseract import os,time chromedriver = "D:\Program Files\Anaconda3\selenium\webdriver\chromedriver.exe" #這里寫本地的chromedriver 的所在路徑 os.environ["webdriver.Chrome.driver"] = chromedriver #調(diào)用chrome瀏覽器 driver = webdriver.Chrome(chromedriver) driver.get("http://xxxx.com") #該處為具體網(wǎng)址 driver.refresh() #刷新頁面 driver.maximize_window() #瀏覽器最大化 #獲取全屏圖片,并截取驗證碼圖片的位置 driver.get_screenshot_as_file('a.png') location = driver.find_element_by_id('imgValidateCode').location size = driver.find_element_by_id('imgValidateCode').size left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] a = Image.open("a.png") im = a.crop((left,top,right,bottom)) im.save('a.png') time.sleep(1) #打開保存的驗證碼圖片 image = Image.open("a.png") #圖片轉(zhuǎn)換成字符 vcode = pytesseract.image_to_string(image) print(vcode) #填充用戶名 密碼 驗證碼 driver.find_element_by_id("staffCode").send_keys("username") driver.find_element_by_id("pwd").send_keys("password") driver.find_element_by_id("validateCode").send_keys(vcode) #點擊登錄 driver.find_element_by_id("loginBtn").click()
總結(jié)
以上所述是小編給大家介紹的Python實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 使用 Python 和 Selenium 解決 Cloudflare 驗證碼的問題
- python+selenium行為鏈登錄12306(滑動驗證碼滑塊)
- Python Selenium破解滑塊驗證碼最新版(GEETEST95%以上通過率)
- Python +Selenium解決圖片驗證碼登錄或注冊問題(推薦)
- Selenium+Python 自動化操控登錄界面實例(有簡單驗證碼圖片校驗)
- selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取功能
- Python Selenium Cookie 繞過驗證碼實現(xiàn)登錄示例代碼
- python+selenium識別驗證碼并登錄的示例代碼
- Python爬蟲selenium驗證之中文識別點選+圖片驗證碼案例(最新推薦)
相關(guān)文章
Python實現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01Python 循環(huán)終止語句的三種方法小結(jié)
今天小編就為大家分享一篇Python 循環(huán)終止語句的三種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python中使用 xlwt 操作excel的常見方法與問題
這篇文章主要給大家介紹了關(guān)于python中使用 xlwt 操作excel的常見方法與問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Python調(diào)用http-post接口的實現(xiàn)方式
這篇文章主要介紹了Python調(diào)用http-post接口的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08pytorch和numpy默認(rèn)浮點類型位數(shù)詳解
這篇文章主要介紹了pytorch和numpy默認(rèn)浮點類型位數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02修改python plot折線圖的坐標(biāo)軸刻度方法
今天小編就為大家分享一篇修改python plot折線圖的坐標(biāo)軸刻度方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12