Python 實現(xiàn)自動登錄+點擊+滑動驗證功能
需要用到的庫有selenium,還需要安裝Chrome瀏覽器驅動,具體如何安裝我就不詳述了
在這里我模擬了csdn的登錄過程
**
1**.首先打開網(wǎng)頁,用戶名+密碼登錄,然后定位用戶名輸入框,和密碼輸入框,輸入后 點擊登陸 彈出驗證滑動條

def __init__(self):
self.url = 'https://passport.csdn.net/login'
self.browser = webdriver.Chrome()
#獲取登錄按鈕對象 選擇 賬號密碼登錄
def get_pass_button(self):
button= self.browser.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[2]/div[5]/ul/li[2]/a')
return button
#打開網(wǎng)址,輸入用戶名。密碼
def open(self,username,password):
self.browser.get(self.url)
self.get_pass_button().click()
2.然后跳轉到登錄視圖

self.browser.find_element_by_xpath('//*[@id="all"]').send_keys(username)
self.browser.find_element_by_xpath('//*[@id="password-number"]').send_keys(password)
3.滑動驗證條:

ps:個人覺得,這個通過用鼠標事件拖動驗證條的方法同樣可以適用于滑動驗證碼,可以把整個滑動驗證碼分為3-4等份,然后寫個循環(huán)每次拖動1/3,基本上3-4次就能通過驗證,這樣就不用用網(wǎng)上寫的那種通過獲取原圖,缺圖的方法,很實用,很適合初學者,個人建議,大佬們別噴…
# 獲取拖拽的滑動驗證碼塊
# 按鈕xpath
slideblock = self.browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
# 鼠標點擊滑動塊不松開
ActionChains(self.browser).click_and_hold(slideblock).perform()
# 將圓球滑至相對起點位置的 右邊xx
ActionChains(self.browser).move_by_offset(xoffset=260, yoffset=0).perform()
time.sleep(10)
# 放開滑動塊
ActionChains(self.browser).release(slideblock).perform()
# time.sleep(10)
整體代碼如下:
#coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class Login():
#打開瀏覽器驅動
def __init__(self):
self.url = 'https://passport.csdn.net/login'
self.browser = webdriver.Chrome()
#獲取登錄按鈕對象 選擇 賬號密碼登錄
def get_pass_button(self):
button= self.browser.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[2]/div[5]/ul/li[2]/a')
return button
#打開網(wǎng)址,輸入用戶名。密碼
def open(self,username,password):
self.browser.get(self.url)
self.get_pass_button().click()
self.browser.find_element_by_xpath('//*[@id="all"]').send_keys(username)
self.browser.find_element_by_xpath('//*[@id="password-number"]').send_keys(password)
#調用 open方法,輸入用戶名。密碼,
#調用 get_geetest_button方法,點擊按鈕
def log(self):
# 輸入用戶名密碼
self.open('33289317','1111')
# 點擊登錄按鈕
self.browser.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[2]/div[5]/div/div[6]/div/button').click()
time.sleep(5)
# 獲取拖拽的滑動驗證碼塊
# 按鈕xpath
slideblock = self.browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
# 鼠標點擊滑動塊不松開
ActionChains(self.browser).click_and_hold(slideblock).perform()
# 將圓球滑至相對起點位置的 右邊xx
ActionChains(self.browser).move_by_offset(xoffset=260, yoffset=0).perform()
time.sleep(10)
# 放開滑動塊
ActionChains(self.browser).release(slideblock).perform()
# time.sleep(10)
#關閉瀏覽器,釋放資源
# self.browser.close()
# 程序主入口
if __name__ == '__main__':
login = Login()
login.log()
總結
到此這篇關于Python 實現(xiàn)自動登錄+點擊+滑動驗證的文章就介紹到這了,更多相關Python 實現(xiàn)自動登錄+點擊+滑動驗證內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決tf.keras.models.load_model加載模型報錯問題
這篇文章主要介紹了解決tf.keras.models.load_model加載模型報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
python multiprocessing 多進程并行計算的操作
這篇文章主要介紹了python multiprocessing 多進程并行計算的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python實現(xiàn)五子棋聯(lián)機對戰(zhàn)小游戲
本文主要介紹了通過Python實現(xiàn)簡單的支持聯(lián)機對戰(zhàn)的游戲——支持局域網(wǎng)聯(lián)機對戰(zhàn)的五子棋小游戲。廢話不多說,快來跟隨小編一起學習吧2021-12-12

