案例:selenium實現登陸百度(若有驗證碼,須要手動輸入)

func.pyhtml

https://www.cnblogs.com/andy9468/p/10899508.htmlpython

 

baidu_login.py中(若有驗證碼,須要手動輸入)web

# 導入webdriver
import os
import time

import re
from lxml import etree

from selenium import webdriver
from func import base642str, str2base64

# 將mm隱匿化
# import sys
# print(str2base64("123456"))
# str1_base64="MTIzNDU2"
# print(base642str(str1_base64))
# sys.exit()

print('000-正在啓用selenium...')
# 調用環境變量指定的PhantomJS瀏覽器建立瀏覽器對象
driver = webdriver.Chrome(r'D:\zzl\new_work\tmp\test01\baidulogin\chromedriver.exe')
print('000-OK')
#
url = 'https://passport.baidu.com/v2/?login'
print('111-selenium正在請求頁面:%s' % url)
driver.get(url)  # get方法請求頁面,獲取響應
print('111-請求OK')

print("打印標題")
print(driver.title)

# 點擊帳號和密碼登陸
input_box0 = driver.find_element_by_xpath("//p[@id='TANGRAM__PSP_3__footerULoginBtn']")
input_box0.click()

print('222-selenium正在填寫表單...')
time.sleep(1)
account = "abcdef"
pwd = "MTIzNDU2"
verify_code = "1234"

input_box1 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__userName']")
input_box1.send_keys(account)
time.sleep(0.5)
input_box2 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__password']")
input_box2.send_keys(base642str(pwd))
time.sleep(0.5)

# 判斷是否須要驗證碼
imgsrc = driver.find_element_by_id("TANGRAM__PSP_3__verifyCodeImg").get_attribute('src')
if re.match(r'https://passport.baidu.com/cgi-bin/genimage.*', imgsrc):
    print("須要驗證碼")
    # 自動填寫驗證碼
    # input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
    # input_box3.send_keys(verify_code)

    # 手動填寫驗證碼
    man_verify_code = input("請填寫驗證碼:")
    input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
    input_box3.send_keys(man_verify_code)
    print("驗證碼填寫完成請等待")
else:
    print("不須要驗證碼")

print('222-填寫表單OK')
time.sleep(1)

print('333-selenium提交表單...')
one_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__submit']")
one_click.click()
time.sleep(1)

# 判斷是否須要手機號綁定確認
try:
    # 綁定手機號確認
    one2_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_27__rebindGuideCancel']")
    one2_click.click()
except Exception as e:
    # print(e)
    print("無需綁定手機號確認")
    pass
time.sleep(2)

print('333-登陸成功...')

  

登陸百度升級版:應對安全驗證chrome

# 導入webdriver
import os
import time

import re
from lxml import etree

from selenium import webdriver
from func import base642str, str2base64

# 將mm隱匿化
# import sys
# # print(str2base64("123456"))
# # str1_base64="MTIzNDU2"
# # print(base642str(str1_base64))
# print()
# sys.exit()

print('000-正在啓用selenium...')
# 調用環境變量指定的PhantomJS瀏覽器建立瀏覽器對象
driver = webdriver.Chrome(r'D:\zzl\new_work\tmp\test01\baidulogin\chromedriver.exe')
print('000-OK')
#
url = 'https://passport.baidu.com/v2/?login'
print('111-selenium正在請求頁面:%s' % url)
driver.get(url)  # get方法請求頁面,獲取響應
print('111-請求OK')

print("打印標題")
print(driver.title)

# 點擊帳號和密碼登陸
input_box0 = driver.find_element_by_xpath("//p[@id='TANGRAM__PSP_3__footerULoginBtn']")
input_box0.click()

print('222-selenium正在填寫表單...')
time.sleep(1)
account = "abcdef"
pwd = "MTIzNDU2"
verify_code = "1234"

input_box1 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__userName']")
input_box1.send_keys(account)
time.sleep(0.5)
input_box2 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__password']")
input_box2.send_keys(base642str(pwd))
time.sleep(0.5)

# 判斷是否須要驗證碼
imgsrc = driver.find_element_by_id("TANGRAM__PSP_3__verifyCodeImg").get_attribute('src')
if re.match(r'https://passport.baidu.com/cgi-bin/genimage.*', imgsrc):
    print("須要驗證碼")
    # 自動填寫驗證碼
    # input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
    # input_box3.send_keys(verify_code)

    # 手動填寫驗證碼
    man_verify_code = input("請填寫驗證碼:")
    input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
    input_box3.send_keys(man_verify_code)
    print("驗證碼填寫完成請等待")
else:
    print("不須要驗證碼")

print('222-填寫表單OK')
time.sleep(1)

print('333-selenium提交表單...')
one_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__submit']")
one_click.click()
time.sleep(2)

# 處理彈窗(手機驗證碼):點擊關閉按鈕
i = 0
while True:  # 處理手機驗證碼認證反覆彈窗
    try:
        one3_click = driver.find_element_by_id("TANGRAM__%s__header_a" % (22 + i))  # 22+i在應對彈窗的關閉按鈕id名稱發生變化。
    except Exception as e:
        print("無需手機驗證碼")
        break
    else:
        print("第 %s 次彈出安全驗證,要求獲取手機驗證碼" % (i + 1))
        time.sleep(0.5)
        print("1s後自動選擇無需手機驗證碼")
        time.sleep(1)
        one3_click.click()
        print("1s後自動點擊登錄")
        time.sleep(1)
        one_click.click()
        time.sleep(2)
        # 判斷是否成功登錄
        current_title = driver.title.strip()
        if current_title.startswith("登陸"):
            print('333-登錄失敗...')
            time.sleep(0.5)
            print('333-2秒後自動重試...')
            i = i + 1
            time.sleep(2)
            continue
        else:
            print("打印標題")
            print(driver.title)
            print('333-登陸成功...')

            import sys

            sys.exit()

# 處理彈窗(綁定手機號確認):點擊不須要修改
# 判斷是否須要手機號綁定確認
try:
    # 綁定手機號確認
    one2_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_27__rebindGuideCancel']")
except Exception as e:
    print("無需綁定手機號確認")
else:
    print("彈出了綁定手機號確認,1s後自動選擇不須要")
    time.sleep(1)
    one2_click.click()

# 打印標題
print("打印標題")
print(driver.title)
time.sleep(2)

# 判斷是否成功登錄
current_title = driver.title.strip()
if current_title.startswith("登陸"):
    print('333-登錄失敗...')
else:
    print('333-登陸成功...')

  

登陸正常輸出:瀏覽器

000-正在啓用selenium...
000-OK
111-selenium正在請求頁面:https://passport.baidu.com/v2/?login
111-請求OK
打印標題
登陸百度賬號
222-selenium正在填寫表單...
不須要驗證碼
222-填寫表單OK
333-selenium提交表單...
第 1 次彈出安全驗證,要求獲取手機驗證碼
1s後自動選擇無需手機驗證碼
1s後自動點擊登錄
333-登錄失敗...
333-2秒後自動重試...
第 2 次彈出安全驗證,要求獲取手機驗證碼
1s後自動選擇無需手機驗證碼
1s後自動點擊登錄
打印標題
賬號設置
333-登陸成功...

  

登陸失敗輸出:安全

000-正在啓用selenium...
000-OK
111-selenium正在請求頁面:https://passport.baidu.com/v2/?login
111-請求OK
打印標題
登陸百度賬號
222-selenium正在填寫表單...
不須要驗證碼
222-填寫表單OK
333-selenium提交表單...
無需手機驗證碼
無需綁定手機號確認
打印標題
登陸百度賬號
333-登錄失敗...

  

 

參考:ide

selenium 帶驗證碼的自動登陸ui

https://blog.csdn.net/chengjintao1121/article/details/82055298url

Python selenium自動化識別驗證碼模擬登陸操做(二).net

https://blog.csdn.net/kk185800961/article/details/78747595

相關文章
相關標籤/搜索