這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)模擬瀏覽器啟動(dòng),獲取網(wǎng)頁(yè)內(nèi)容、自動(dòng)填表單、自動(dòng)登錄、自動(dòng)過驗(yàn)證碼等功能,需要的可以參考一下
-
庫(kù)
-
源碼
-
知識(shí)點(diǎn)補(bǔ)充
食用前準(zhǔn)備
python 3.10.10 #二維碼的庫(kù)ddddocr 需要
庫(kù)
import time
import ddddocr
源碼?????
# import threading # 導(dǎo)入threading模塊
# from Feishu_SendMsg import *
# Identification verification code
import time
import ddddocr
interval = 100 * 60
# def delayCall(): # 定義方法
# SendMsg("選題 快快快!!!")
# timer=threading.Timer(interval,delayCall) # 每秒運(yùn)行
# timer.start() # 執(zhí)行方法
# if __name__ == '__main__': #
# t1=threading.Timer(interval,function=delayCall) # 創(chuàng)建定時(shí)器
# t1.start() # 開始執(zhí)行線程
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
# SendMsg("自動(dòng)填表單")
options = webdriver.ChromeOptions()
options.add_argument('--enable-automation')
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--start-maximized')
options.add_argument('--disable-infobars')
prefs = {"profile.default_content_setting_values.autocomplete_enabled": 2}
options.add_experimental_option("prefs", prefs)
# SendMsg("創(chuàng)建 Chrome 瀏覽器實(shí)例")
# 創(chuàng)建 Chrome 瀏覽器實(shí)例
browser = webdriver.Chrome(options=options)
# SendMsg("打開網(wǎng)頁(yè)")
browser.get('www.tttttttt.com')
# SendMsg("找到賬號(hào)和密碼框元素并輸入指定字符串")
username = browser.find_element("name","username")
password = browser.find_element("name","userpass")
usercode = browser.find_element("name","usercode")
img_verifycode = browser.find_element("id","img_verifycode")
# SendMsg("自動(dòng)填充賬號(hào)密碼")
username.send_keys("11111")
password.send_keys("11111")
verifycodeBase64 = img_verifycode.screenshot_as_base64
ocr = ddddocr.DdddOcr()
res = ocr.classification(verifycodeBase64)
usercode.send_keys(res)
# SendMsg(f"識(shí)別并填寫驗(yàn)證碼: {res}")
# SendMsg("提交表單")
password.send_keys(Keys.RETURN)
# SendMsg("登陸: 提交表單")
知識(shí)點(diǎn)補(bǔ)充
下面為大家介紹一下文中用到的ddddocr庫(kù)的相關(guān)使用吧
識(shí)別驗(yàn)證碼的python 庫(kù)有很多,用起來也并不簡(jiǎn)單,ddddocr (帶帶弟弟ocr)庫(kù)是一個(gè)簡(jiǎn)單實(shí)用的識(shí)別驗(yàn)證碼的庫(kù),推薦給大家
ddddocr具體使用方法???????
import os
import ddddocr
from time import sleep
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
class GetVerificationCode:
def __init__(self):
self.res = None
url = '要登錄的地址'
self.driver = webdriver.Chrome()
self.driver.maximize_window() # 將瀏覽器最大化
self.driver.get(url)
# 獲取驗(yàn)證碼信息
def getVerification(self):
# 獲取當(dāng)前文件的位置、并獲取保存截屏的位置
current_location = os.path.dirname(__file__)
screenshot_path = os.path.join(current_location, "..", "VerificationCode")
# 截取當(dāng)前網(wǎng)頁(yè)并放到自定義目錄下,并命名為printscreen,該截圖中有我們需要的驗(yàn)證碼
sleep(1)
self.driver.save_screenshot(screenshot_path + '//' + 'printscreen.png')
sleep(1)
# 定位驗(yàn)證碼
imgelement = self.driver.find_element(By.XPATH, '驗(yàn)證碼圖片的Xpath定位')
# 獲取驗(yàn)證碼x,y軸坐標(biāo)
location = imgelement.location
# 獲取驗(yàn)證碼的長(zhǎng)寬
size = imgelement.size
# 寫成我們需要截取的位置坐標(biāo)
rangle = (int(location['x'] + 430),
int(location['y'] + 200),
int(location['x'] + size['width'] + 530),
int(location['y'] + size['height'] + 250))
# 打開截圖
i = Image.open(screenshot_path + '//' + 'printscreen.png')
# 使用Image的crop函數(shù),從截圖中再次截取我們需要的區(qū)域
fimg = i.crop(rangle)
fimg = fimg.convert('RGB')
# 保存我們截下來的驗(yàn)證碼圖片,并讀取驗(yàn)證碼內(nèi)容
fimg.save(screenshot_path + '//' + 'code.png')
ocr = ddddocr.DdddOcr()
with open(screenshot_path + '//' + 'code.png', 'rb') as f:
img_bytes = f.read()
self.res = ocr.classification(img_bytes)
print('識(shí)別出的驗(yàn)證碼為:' + self.res)
# 判斷驗(yàn)證碼錯(cuò)誤時(shí)的提示信息是否存在
def isElementPresent(self, by, value):
try:
element = self.driver.find_element(by=by, value=value)
except NoSuchElementException:
pass
# 發(fā)生了NoSuchElementException異常,說明頁(yè)面中未找到該元素,返回False
return False
else:
# 沒有發(fā)生異常,表示在頁(yè)面中找到了該元素,返回True
return True
# 登錄
def login(self):
self.getVerification()
self.driver.find_element(By.XPATH, '用戶名輸入框Xpath定位').send_keys('用戶名')
self.driver.find_element(By.XPATH, '密碼輸入框Xpath定位').send_keys('密碼')
self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click()
sleep(2)
isFlag = True
while isFlag:
try:
isPresent = self.isElementPresent(By.XPATH, '驗(yàn)證碼錯(cuò)誤時(shí)的提示信息Xpath定位')
if isPresent is True:
codeText = self.driver.find_element(By.XPATH, '驗(yàn)證碼錯(cuò)誤時(shí)的提示信息Xpath定位').text
if codeText == "驗(yàn)證碼不正確":
self.getVerification()
sleep(2)
self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').clear()
sleep(1)
self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click()
sleep(2)
tips = self.driver.find_element(By.XPATH,
'未輸入驗(yàn)證碼時(shí)的提示信息Xpath定位').text
if tips == "請(qǐng)輸入驗(yàn)證碼":
self.getVerification()
sleep(2)
self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').click()
sleep(1)
self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click()
sleep(2)
continue
else:
print("驗(yàn)證碼正確,登錄成功!")
except NoSuchElementException:
pass
else:
isFlag = False
sleep(5)
self.driver.quit()
if __name__ == '__main__':
GetVerificationCode().login()
識(shí)別結(jié)果
?到此這篇關(guān)于Python實(shí)現(xiàn)獲取網(wǎng)頁(yè)內(nèi)容及自動(dòng)填表單與登錄功能的文章就介紹到這了,希望大家以后多多支持!
最后:?下方這份完整的軟件測(cè)試視頻學(xué)習(xí)教程已經(jīng)整理上傳完成,朋友們?nèi)绻枰梢宰孕忻赓M(fèi)領(lǐng)取【保證100%免費(fèi)】
?這些資料,對(duì)于【軟件測(cè)試】的朋友來說應(yīng)該是最全面最完整的備戰(zhàn)倉(cāng)庫(kù),這個(gè)倉(cāng)庫(kù)也陪伴上萬個(gè)測(cè)試工程師們走過最艱難的路程,希望也能幫助到你!文章來源:http://www.zghlxwxcb.cn/news/detail-513735.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-513735.html
到了這里,關(guān)于Python實(shí)現(xiàn)獲取網(wǎng)頁(yè)內(nèi)容及自動(dòng)填表單與登錄功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!