1.引入Chrome驅動,打開qq空間網頁html
bro = webdriver.Chrome(executable_path='./chromedriver.exe') bro.get('https://qzone.qq.com/')
2.因爲進入以後首先提示的是掃描二維碼所以要切換到帳戶密碼登陸python
首先找到帳戶密碼登陸所對應的標籤web
以後觸發點擊 chrome
a_tag = bro.find_element_by_id('switcher_plogin') a_tag.click()
再以後找到帳戶密碼所對應的的input標籤的id瀏覽器
userName_tag = bro.find_element_by_id('u') passWord_tag = bro.find_element_by_id('p')
三、輸入帳戶密碼ui
userName_tag.send_keys("7xxxxx") passWord_tag.send_keys('xxxxx.')
4.找到登陸按鈕所對應的的標籤idspa
btn = bro.find_element_by_id("login_button") btn.click()
5.點擊登陸後會切換到滑動驗證窗口,這個驗證窗口是嵌套在iframe標籤裏,所以須要先切換到驗證框所對應的的iframe標籤code
iframe = bro.find_element_by_xpath('//iframe') # 找到「嵌套」的iframe,開頭//表示從當前節點尋找全部的後代元素,當前在iframe 須要往下嵌套的iframe bro.switch_to.frame(iframe) # 切換到iframe
拖動距離的能夠經過拖動滑塊觀察下圖中所示數值減去初始值來肯定距離orm
button = bro.find_element_by_id('tcaptcha_drag_button') # 尋找滑塊 print("尋找滑塊") sleep(1) print("開始拖動") # 開始拖動 perform()用來執行ActionChains中存儲的行爲 distance = 175 action = ActionChains(bro) action.reset_actions() # 清除以前的action action.click_and_hold(button).perform() # click_and_hold 點擊並保持 action.move_by_offset(distance, 0).perform() action.release().perform() # 釋放action
6.完整代碼:htm
from selenium import webdriver from time import sleep from selenium.webdriver import ActionChains bro = webdriver.Chrome(executable_path='./chromedriver.exe') bro.get('https://qzone.qq.com/') bro.switch_to.frame('login_frame') a_tag = bro.find_element_by_id('switcher_plogin') a_tag.click() userName_tag = bro.find_element_by_id('u') passWord_tag = bro.find_element_by_id('p') sleep(1) userName_tag.send_keys("7xxxxx") sleep(1) passWord_tag.send_keys('xxxxx.') btn = bro.find_element_by_id("login_button") btn.click() sleep(1) iframe = bro.find_element_by_xpath('//*[@id="tcaptcha_iframe"]') # 找到「嵌套」的iframe bro.switch_to.frame(iframe) # 切換到iframe sleep(2) button = bro.find_element_by_id('tcaptcha_drag_button') # 尋找滑塊 print("尋找滑塊") sleep(1) print("開始拖動") # 開始拖動 perform()用來執行ActionChains中存儲的行爲 distance = 175 action = ActionChains(bro) action.reset_actions() # 清除以前的action action.click_and_hold(button).perform() # click_and_hold 點擊並保持 action.move_by_offset(distance, 0).perform() action.release().perform() # 釋放action sleep(60) # 退出瀏覽器 bro.quit()
原文出處:https://www.cnblogs.com/kakawith/p/12395027.html