1.引入Chrome驅動,打開qq空間網頁
bro = webdriver.Chrome(executable_path='./chromedriver.exe') bro.get('https://qzone.qq.com/')
2.由於進入之后首先提示的是掃描二維碼因此要切換到賬戶密碼登錄
首先找到賬戶密碼登錄所對應的標簽
之后觸發點擊
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')
3、輸入賬戶密碼
userName_tag.send_keys("7xxxxx") passWord_tag.send_keys('xxxxx.')
4.找到登錄按鈕所對應的的標簽id
btn = bro.find_element_by_id("login_button") btn.click()
5.點擊登錄后會切換到滑動驗證窗口,這個驗證窗口是嵌套在iframe標簽里,因此需要先切換到驗證框所對應的的iframe標簽
iframe = bro.find_element_by_xpath('//iframe') # 找到“嵌套”的iframe,開頭//表示從當前節點尋找所有的后代元素,當前在iframe 需要往下嵌套的iframe bro.switch_to.frame(iframe) # 切換到iframe
拖動距離的可以通過拖動滑塊觀察下圖中所示數值減去初始值來確定距離
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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.完整代碼:
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()