處理快顯視窗在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)
關閉快顯視窗,回到最初的視窗
# 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())