須要用到的庫有selenium,還須要安裝Chrome瀏覽器驅動,具體如何安裝我就不詳述了web
在這裏我模擬了csdn的登陸過程瀏覽器
**app
1**.首先打開網頁,用戶名+密碼登陸,而後定位用戶名輸入框,和密碼輸入框,輸入後 點擊登錄 彈出驗證滑動條ide
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 #打開網址,輸入用戶名。密碼 def open(self,username,password): self.browser.get(self.url) self.get_pass_button().click()
2.而後跳轉到登陸視圖url
self.browser.find_element_by_xpath('//*[@id="all"]').send_keys(username) self.browser.find_element_by_xpath('//*[@id="password-number"]').send_keys(password)
3.滑動驗證條:spa
ps:我的以爲,這個經過用鼠標事件拖動驗證條的方法一樣能夠適用於滑動驗證碼,能夠把整個滑動驗證碼分爲3-4等份,而後寫個循環每次拖動1/3,基本上3-4次就能經過驗證,這樣就不用用網上寫的那種經過獲取原圖,缺圖的方法,很實用,很適合初學者,我的建議,大佬們別噴….net
# 獲取拖拽的滑動驗證碼塊 # 按鈕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)
總體代碼以下:orm
#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 #打開網址,輸入用戶名。密碼 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 實現自動登陸+點擊+滑動驗證的文章就介紹到這了,更多相關Python 實現自動登陸+點擊+滑動驗證內容請搜索之前的文章或繼續瀏覽下面的相關文章但願你們之後多多支持!blog