How to switch to new window in Selenium for Python?

后端 未结 5 1769
梦谈多话
梦谈多话 2020-11-22 13:16

I am working on selenium automation project using Python.

I am facing an issue, which is handling multiple browser windows.

Scenario is as f

相关标签:
5条回答
  • 2020-11-22 13:33

    You can do it by using window_handles and switch_to_window method.

    Before clicking the link first store the window handle as

    window_before = driver.window_handles[0]
    

    after clicking the link store the window handle of newly opened window as

    window_after = driver.window_handles[1]
    

    then execute the switch to window methow to move to newly opened window

    driver.switch_to_window(window_after)
    

    and similarly you can switch between old and new window. Following is the code example

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    
    class GoogleOrgSearch(unittest.TestCase):
    
         def setUp(self):
             self.driver = webdriver.Firefox()
    
        def test_google_search_page(self):
             driver = self.driver
             driver.get("http://www.cdot.in")
             window_before = driver.window_handles[0]
             print window_before
             driver.find_element_by_xpath("//a[@href='http://www.cdot.in/home.htm']").click()
             window_after = driver.window_handles[1]
             driver.switch_to_window(window_after)
             print window_after
             driver.find_element_by_link_text("ATM").click()
             driver.switch_to_window(window_before)
    
    
        def tearDown(self):
        self.driver.close()
    
    if __name__ == "__main__":
    unittest.main()
    
    0 讨论(0)
  • 2020-11-22 13:49

    for eg. you may take

    driver.get('https://www.naukri.com/')
    

    since, it is a current window ,we can name it

    main_page = driver.current_window_handle
    

    if there are atleast 1 window popup except the current window,you may try this method and put if condition in break statement by hit n trial for the index

    for handle in driver.window_handles:
        if handle != main_page:
            print(handle)
            login_page = handle
            break
    
    driver.switch_to.window(login_page)
    

    Now ,whatever the credentials you have to apply,provide after it is loggen in. Window will disappear, but you have to come to main page window and you are done

    driver.switch_to.window(main_page)
    sleep(10)
    
    0 讨论(0)
  • 2020-11-22 13:50

    On top of the answers already given, to open a new tab the javascript command window.open() can be used.

    For example:

    # Opens a new tab
    self.driver.execute_script("window.open()")
    
    # Switch to the newly opened tab
    self.driver.switch_to.window(self.driver.window_handles[1])
    
    # Navigate to new URL in new tab
    self.driver.get("https://google.com")
    # Run other commands in the new tab here
    

    You're then able to close the original tab as follows

    # Switch to original tab
    self.driver.switch_to.window(self.driver.window_handles[0])
    
    # Close original tab
    self.driver.close()
    
    # Switch back to newly opened tab, which is now in position 0
    self.driver.switch_to.window(self.driver.window_handles[0])
    

    Or close the newly opened tab

    # Close current tab
    self.driver.close()
    
    # Switch back to original tab
    self.driver.switch_to.window(self.driver.window_handles[0])
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 13:54

    We can handle the different windows by moving between named windows using the “switchTo” method:

    driver.switch_to.window("windowName")
    
    <a href="somewhere.html" target="windowName">Click here to open a new window</a>
    

    Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

    for handle in driver.window_handles:
        driver.switch_to.window(handle)
    
    0 讨论(0)
  • 2020-11-22 13:56

    window_handles should give you the references to all open windows.

    this is what the docu has to say about switching windows.

    0 讨论(0)
提交回复
热议问题