Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch to new window in Selenium for Python?

I am working on selenium automation project using Python.

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

Scenario is as follows. When I click a link on the home page, a new window opens. In the newly opened window I cannot perform any actions, because the focus is still on the home page web driver.

Can anybody show me how to change focus from the background window to the newly opened window?

A possible solution is driver.switch_to.window(), but it requires the window's name. How to find out the window's name? If this is a wrong way to do this, can anybody give some code examples to perform this action?

like image 910
Sandeep Raveendran Avatar asked May 17 '12 04:05

Sandeep Raveendran


People also ask

How do I switch between windows in python Selenium?

window_handles is used for working with different windows. It stores the window ids that are used for switching. switch_to. window method is used for switching between the windows with the help of window_handles ids.

How do I switch between tabs in python Selenium?

We can switch different browser tabs using Selenium webdriver in Python using the method switch_to. window. By default, the webdriver has access to the parent window. Once another browser tab is opened, the switch_to.

How do I move to a new window in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

How do I switch between multiple windows in Selenium?

In Selenium, when we have multiple windows in any web application, the approach may need to switch control among several windows i.e from one window to another to perform any action and we can achieve this by using switchto(); method.


2 Answers

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.

like image 30
Elliot Ledger Avatar answered Sep 27 '22 20:09

Elliot Ledger


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 method 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  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() 
like image 87
vishy dewangan Avatar answered Sep 27 '22 19:09

vishy dewangan