How to Switch to New Window in Selenium for Python

Selenium/Python: How do I move to a new window with no window name given?

Your driver instance should have a list named window_handles. You should be able to save the current and desired window handles and switch between them using driver.switch_to_window:

main_window = driver.window_handles[0]
new_window = driver.window_handles[1]
driver.switch_to_window(new_window)
# Do something
driver.switch_to_window(main_window)

Python selenium - How to switch to next window

Based on your problem description, it's hard to tell if you need to use window handles at all for this. You did mention you tried to use driver.window_handles, but all you mentioned was that you were facing an issue. Since I don't know what that issue is, I am making a few assumptions here. These two lines of code:

driver.find_element_by_id("submit").click()   
firefox_elem = driver.find_element_by_tag_name('html') --> login to main page is working fine
******<missing piece: How to select a value from dropdown in next page>******

do not necessarily mean firefox_elem = driver.find_element_by_tag_name('html') is working as intended. You are just searching for top-level html tag, but if driver is focused on the previous window handle, this call will not throw any errors. It's not really checking for anything here.

I would use window_handles and switch_to_window to try and switch to your new window that is opened:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

# switch to newly opened window -- the index varies based on number of pages opened.
driver.switch_to_window(driver.window_handles[1])

# click button to expand dropdown (wait on it to exist first)
dropdown_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[@data-toggle='dropdown']")))
dropdown_button.click()

# click the option with text "Last 1 year" (wait on it to exist first)
dropdown_option = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//ul/li/a[text()='Last 1 year']")))
dropdown_option.click()

This will click the dropdown item with text 'Last 1 year'.

I would also recommend trying the following:

driver.find_element_by_id("submit").click()   
firefox_elem = driver.find_element_by_tag_name('html') --> login to main page is working

# switch to newly opened window -- the index varies based on number of pages opened.
driver.switch_to_window(driver.window_handles[1])

print(driver.page_source)

to see what driver thinks your page source is. This will help us determine what the driver is currently focused on, so that we can determine whether or not we need to utilize driver.window_handles and driver.switch_to_window.

How do I switch to the active tab in Selenium?

Some possible approaches:

1 - Switch between the tabs using the send_keys (CONTROL + TAB)

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

2 - Switch between the tabs using the using ActionsChains (CONTROL+TAB)

actions = ActionChains(self.driver)      
actions.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()

3 - Another approach could make usage of the Selenium methods to check current window and move to another one:

You can use

driver.window_handles

to find a list of window handles and after try to switch using the following methods.

- driver.switch_to.active_element      
- driver.switch_to.default_content
- driver.switch_to.window

For example, to switch to the last opened tab, you can do:

driver.switch_to.window(driver.window_handles[-1])

How to open a new window on a browser using Selenium WebDriver for python?

How about you do something like this

driver = webdriver.Firefox() #First FF window
second_driver = webdriver.Firefox() #The new window you wanted to open

Depending on which window you want to interact with, you send commands accordingly

print driver.title #to interact with the first driver
print second_driver.title #to interact with the second driver

For all down voters:


The OP asked for "it is only important that a second instance of the browser is opened.". This answer does not encompass ALL possible requirements of each and everyone's use cases.
The other answers below may suit your particular need.

Open web in new tab Selenium + Python

Editor's note: This answer no longer works for new Selenium versions. Refer to this comment.


You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

In selenium you can emulate such behavior.
You will need to create one webdriver and as many tabs as the tests you need.

Here it is the code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.google.com/")

#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs

# Load a page
driver.get('http://stackoverflow.com/')
# Make the tests...

# close the tab
# (Keys.CONTROL + 'w') on other OSs.
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')

driver.close()


Related Topics



Leave a reply



Submit