selenium Webdriver多窗口切換


應用場景:

在頁面操作過程中有時候點擊某個鏈接會彈出新的窗口,這時候就需要主機切換到新打開的窗口上進行操作。WebDriver提供了switch_to.window()方法,可以實現在不同的窗口直接切換。
以百度首頁和百度注冊頁為例,在兩個窗口直接的切換如圖

 

代碼如下:

 1 #導包
 2 from selenium import webdriver
 3 from time import sleep
 4 #定義瀏覽器句柄,打開百度網址
 5 driver=webdriver.Chrome()
 6 driver.implicitly_wait(10)
 7 url="http://www.baidu.com"
 8 driver.get(url)
 9 #窗口最大化
10 driver.maximize_window()
11 #獲取百度搜索窗口的句柄
12 seach_windows=driver.current_window_handle
13 #打印百度搜索窗口的句柄
14 print(seach_windows)
15 #點擊右上角"登錄"按鈕
16 driver.find_element_by_link_text("登錄").click()
17 #在彈窗中點擊"立即注冊"按鈕
18 driver.find_element_by_xpath('//*[@id="passport-login-pop-dialog"]/div/div/div/div[4]/a').click()
19 #獲取當前所有打開窗口的句柄
20 all_handles=driver.window_handles
21 #進入注冊窗口
22 for newhandle in all_handles:
23     if newhandle!=seach_windows:
24         driver.switch_to.window(newhandle)
25         print('now register window!')
26         sleep(1)
27         #輸入用戶名:username12334
28         driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("username12334")
29         #輸入手機號:18877776666
30         driver.find_element_by_id("TANGRAM__PSP_4__phone").send_keys("18877776666")
31         #輸入密碼:password
32         driver.find_element_by_id("TANGRAM__PSP_4__password").send_keys("password")
33         #點擊獲取語音驗證碼
34         driver.find_element_by_id("TANGRAM__PSP_4__verifyCodeSend").click()
35         #輸入驗證碼:123456
36         driver.find_element_by_id("TANGRAM__PSP_4__verifyCode").send_keys("123456")
37         #勾選(閱讀並接受《百度用戶協議》及《百度隱私權保護聲明》)
38         driver.find_element_by_id("TANGRAM__PSP_4__isAgree").click()
39         #點擊"注冊"按鈕
40         driver.find_element_by_id("TANGRAM__PSP_4__submit").click()
41 #回到百度搜索窗口
42 for newhandle in all_handles:
43     if newhandle==seach_windows:
44         driver.switch_to.window(newhandle)
45         print('now seach window!')
46         sleep(1)
47         #關閉登錄/立即注冊的彈窗
48         driver.find_element_by_id("TANGRAM__PSP_4__closeBtn").click()
49         #點擊百度輸入框,輸入"selenium webdriver"
50         driver.find_element_by_xpath('//input[@id="kw"]').send_keys("selenium webdriver")
51         #點擊"百度一下"按鈕
52         driver.find_element_by_xpath('//input[@id="su"]').click()
53         sleep(2)
54 #關閉所有窗口,退出瀏覽器,結束本次腳本任務
55 driver.quit()
 
腳本執行過程:首先打開百度首頁,通過current_window_handle獲得當前窗口的句柄,並賦值給變量seach_windows 接着打開登錄窗口,在登錄彈窗上單擊“立即注冊”,從而打開新的注冊窗口。通過window_handles獲取當前打開的所有窗口的句柄,並賦值給變量all_handles 
第一個for循環遍歷了all_handles,如果newhandle不等於seach_windows,那么一定是注冊窗口,因為腳本執行過程中只打開了兩個窗口。所以,通過switch_to.window()切換到注冊頁面進行注冊操作。第二個for循環類似,不過這一次判斷如果newhandle等於seach_windows,那么切換到百度搜索頁,然后進行搜索操作。
總結:
current_window_handle:獲取當前窗口的句柄
window_handles:返回所有窗口的句柄到當前會話
switch_to.window():用於切換到相應的窗口,與switch_to.frame()類似,前者用於不同窗口的切換,后者用於不同表單之間的切換。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
talk is cheap,show me the code.
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM