Python 實現(xiàn)自動登錄+點擊+滑動驗證功能
需要用到的庫有selenium,還需要安裝Chrome瀏覽器驅(qū)動,具體如何安裝我就不詳述了
在這里我模擬了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.然后跳轉(zhuǎn)到登錄視圖

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:個人覺得,這個通過用鼠標(biāo)事件拖動驗證條的方法同樣可以適用于滑動驗證碼,可以把整個滑動驗證碼分為3-4等份,然后寫個循環(huán)每次拖動1/3,基本上3-4次就能通過驗證,這樣就不用用網(wǎng)上寫的那種通過獲取原圖,缺圖的方法,很實用,很適合初學(xué)者,個人建議,大佬們別噴…
# 獲取拖拽的滑動驗證碼塊
# 按鈕xpath
slideblock = self.browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
# 鼠標(biāo)點擊滑動塊不松開
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():
#打開瀏覽器驅(qū)動
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)
#調(diào)用 open方法,輸入用戶名。密碼,
#調(diào)用 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"]')
# 鼠標(biāo)點擊滑動塊不松開
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)
#關(guān)閉瀏覽器,釋放資源
# self.browser.close()
# 程序主入口
if __name__ == '__main__':
login = Login()
login.log()
總結(jié)
到此這篇關(guān)于Python 實現(xiàn)自動登錄+點擊+滑動驗證的文章就介紹到這了,更多相關(guān)Python 實現(xiàn)自動登錄+點擊+滑動驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python正則表達式去除兩個特殊字符間的內(nèi)容方法
今天小編就為大家分享一篇python正則表達式去除兩個特殊字符間的內(nèi)容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
解決jupyter不是內(nèi)部或外部命令,也不是可運行程序問題
這篇文章主要介紹了解決jupyter不是內(nèi)部或外部命令,也不是可運行程序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python enumerate遍歷數(shù)組示例應(yīng)用
遍歷數(shù)組的python代碼2008-09-09

