capturing or positioning.
pop-up windowThe key is to get a handle to the popup window. I still like to illustrate by example: #-*-coding=utf-8from Selenium Import
WebdriverImport Unittest class Login (unittest. TestCase): def setUp (self): Self.driver = Webdriver. Firefox () self.driver.implicitly_wait (+) Self.base_url = "/http" Www.baidu.com " self.verificationerrors = [] def test_ Loginpass (self): Driver = self.driver Driver.get (Self.base_url) & nbsp nowhandle=driver.current_window_handle# here to get the current window handle Driver.find_ element_by_id ("kw"). Send_keys ("Selenium") driver.find_element_by_id ("su"). Click () Driver.find_element_by_xpath ("//a[@title = ' Selenium installation ']"). Click () aalhandles=driver.window_handles# get all window handles for handle in Aalhandles: #在所有窗口中查找弹出窗口 if Handle!=nowhandle: Driver.switch_to_window (handle) #这两步是在弹出窗口中进行的操作 to prove that we did enter the Driver.find_element_by_link_text ("News"). Click () & nbsp Driver.switch_to_window (nowhandle) #返回到主窗口页面 driver.find_element_by_id ("kw"). Clear () #下面三步是返回到主窗口中进行的操作, proving that we did return driver.find_element_by_id ("kw"). Send_keys ("
python") driver.find_element_by_id (" su "). Click () def tearDown (self): #self. Driver.quit () self.as Sertequal ([], self.verificationerrors) if __name__ = = "__main__": Unittest.main () Note: Webdriver in Python Curren T_window_handle to get the current window function window_handles get all the window functions Switch_to_window () into the specified window function
Selenium +python how to get a popup window