一、打開網頁
url:https://www.lagou.com/
二、關閉彈窗

三、點擊登錄

四、輸入賬號密碼

五、勾選同意協議并登錄

六、超級鷹驗證碼識別

七、判斷是否登錄成功與是否失敗次數過多
判斷是否登錄成功:隨機選擇一個當前頁面沒有的元素,而成功登錄頁面存在的元素.

源代碼
web = Chrome()
web.get("https://www.lagou.com/")
web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click()
sleep(0.5)
# 進入登錄頁面
web.find_element_by_xpath('//*[@id="lg_tbar"]/div[1]/div[2]/ul/li[1]').click()
sleep(0.5)
# 切換至密碼登錄
web.find_element_by_class_name('change-login-type').click()
sleep(0.5)
# 輸入賬戶密碼
web.find_element_by_xpath('//div[@data-propertyname="username"]/div/input').send_keys(
username)
web.find_element_by_xpath('//div[@data-propertyname="password"]/div/input').send_keys(
password)
web.find_element_by_xpath('//div[@class="login-checked"]').click()
# 點擊驗證碼
sleep(0.5)
web.find_element_by_xpath('/html/body/div[3]/div[1]/div/div/div[2]/div[3]/div[2]/div[2]/div[2]').click()
sleep(0.5)
chaojiying = Chaojiying_Client(username,password, '915491')
j = 1
while True:
# 判斷是否發生例外,多次識別失敗
print(f"正在嘗試第{j}次登錄")
j += 1
try:
web.find_element_by_xpath('/html/body//div[@class="geetest_panel_error_content"]').click()
sleep(2)
except:
sleep(1)
img = web.find_element_by_xpath(
'//div[@class="geetest_holder geetest_silver"]/div[@class="geetest_widget"]')
# 超級鷹獲取物品位置
offset_list = chaojiying.PostPic(img.screenshot_as_png, 9004)['pic_str'].split('|')
# 點擊物品
for i in offset_list:
temp = i.split(',')
x = int(temp[0])
y = int(temp[1])
ActionChains(web).move_to_element_with_offset(img, x, y).click().perform()
sleep(1)
web.find_element_by_xpath('//div[@class="geetest_panel"]/a/div').click()
sleep(5)
# 是否成功登錄,成功登錄是,is_login匹配的元素不為空
try:
is_login = web.find_element_by_xpath('//[@id="lg_tbar"]/div[1]/div[2]/ul/li[2]/a')
break
except:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/356790.html
標籤:其他
