处理弹出窗口
在Selenium,弹出窗口是比较棘手的一个问题,下面谈谈利用Python怎么处理弹出窗口。
最简单的方法:创建弹出窗口,然后使用get_all_window_names和select_window方法

 

  1. get_all_window_names(self):  Returns the names of all windows that the browser knows about.
  2. 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.
  3. wait_for_pop_up(self,windowID,timeout): Waits for a popup window to appear and load up.
  4. 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.
  5. 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)

相关文章: