1 # -*- coding:utf-8 -*-
2 """
3 switch_to_window():接受 name 和 handle 属性
4 name 属性和它的 title 是不一样的
5 如下例子中的 name 为: HelpWindow
6 <html>
7 <head>
8 <title>help</title>
9 </head>
10 <body>
11 <button id="helpbutton1" onclick="window.open("PopUpWindow.html","HelpWindow","width=500,height=500");">Help1</button>
12 <button id="helpbutton2" onclick="window.open("PopUpWindow.html","HelpWindow","width=500,height=500");">Help2</button>
13 </body>
14 </html>
15 """
16 from selenium import webdriver
17
18 driver = webdriver.Firefox()
19 driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\window.html')
20
21 #父窗口句柄
22 parentWindowId = driver.current_window_handle
23
24 driver.find_element_by_id('helpbutton1').click()
25
26 #转到 HelpWindow窗口
27 driver.switch_to_window('HelpWindow')
28
29 print driver.title
30
31 assert 'PopUpWwindow', driver.find_element_by_tag_name('p').text
32
33 #关闭子窗口
34 driver.close()
35
36 #回到父窗口
37 driver.switch_to_window(parentWindowId)
38
39 #验证父窗口的title
40 assert True, driver.title == 'help'
41
42 driver.close()