Selenium RC For Python:教程5
处理弹出窗口
在Selenium,弹出窗口是比较棘手的一个问题,下面谈谈利用Python怎么处理弹出窗口。
最简单的方法:创建弹出窗口,然后使用get_all_window_names和select_window方法
- get_all_window_names(self): Returns the names of all windows that the browser knows about.
- select_window(self,windowID): Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use null as the target.
- wait_for_pop_up(self,windowID,timeout): Waits for a popup window to appear and load up.
- select_window(self,windowID): Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use null as the target.
- close(self): Simulates the user clicking the "close" button in the titlebar of a popup window or tab.
代码
def test_popup_creation(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
windowNames = sel.get_all_window_names()
#print windowNames[0]
self.assertEquals(1, len(windowNames))
self.assertEquals("selenium_main_app_window", windowNames[0])
sel.click("link=This link opens a popup")
sel.wait_for_pop_up("popup", self.MAX_WAIT_IN_MS)
windowNames = sel.get_all_window_names()
print windowNames
self.assertEquals(2, len(windowNames))
self.assertTrue("selenium_main_app_window" in windowNames)
self.assertTrue("popup" in windowNames)
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
windowNames = sel.get_all_window_names()
#print windowNames[0]
self.assertEquals(1, len(windowNames))
self.assertEquals("selenium_main_app_window", windowNames[0])
sel.click("link=This link opens a popup")
sel.wait_for_pop_up("popup", self.MAX_WAIT_IN_MS)
windowNames = sel.get_all_window_names()
print windowNames
self.assertEquals(2, len(windowNames))
self.assertTrue("selenium_main_app_window" in windowNames)
self.assertTrue("popup" in windowNames)
关闭弹出窗口,回到最初的窗口
# close popup
sel.close()
# select original window
sel.select_window("null")
代码
def test_window_selection_closepopup_returntomainwindow(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())
sel.click("link=This link opens a popup")
sel.wait_for_pop_up("popup", self.MAX_WAIT_IN_MS)
sel.select_window("popup")
self.assertEquals("Bit Motif", sel.get_title())
# close popup
sel.close()
# select original window
sel.select_window("null")
self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())
sel.click("link=This link opens a popup")
sel.wait_for_pop_up("popup", self.MAX_WAIT_IN_MS)
sel.select_window("popup")
self.assertEquals("Bit Motif", sel.get_title())
# close popup
sel.close()
# select original window
sel.select_window("null")
self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())
作者:Shane
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。