How to Switch to the Active Tab in Selenium

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 switch to active tab in Selenium

Use WebDriver's getWindowHandles() command to switch between the windows.

String currentWindow = driver.getWindowHandle();

for(String newWindows : driver.getWindowHandles()){
driver.switchTo().window(newWindows);
}

// perform some action in new window and close it
driver.close();

// switch to default window or current window
driver.switchTo().window(currentWindow);
// OR
driver.switchTo().defaultContent();

Switch tabs using Selenium WebDriver with Java

    psdbComponent.clickDocumentLink();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));

This code perfectly worked for me. Try it out. You always need to switch your driver to new tab, before you want to do something on new tab.

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()

switch between tabs and perform action on individual using Selenium

You'll need to change focus to the new tab as they usually get opened in the background. Then you'll need to grab a handle for the now current tab.

This is described here:

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep

browser = webdriver.Firefox()
browser.get('https://www.google.com?q=python#q=python')
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc'))
first_link = first_result.find_element_by_tag_name('a')

# Save the window opener (current window, do not mistaken with tab... not the same)
main_window = browser.current_window_handle

# Open the link in a new tab by sending key strokes on the element
# Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack
first_link.send_keys(Keys.CONTROL + Keys.RETURN)

# Switch tab to the new tab, which we will assume is the next one on the right
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

# Put focus on current window which will, in fact, put focus on the current visible tab
browser.switch_to_window(main_window)

# do whatever you have to do on this page, we will just got to sleep for now
sleep(2)

# Close current tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

# Put focus on current window which will be the window opener
browser.switch_to_window(main_window)


Related Topics



Leave a reply



Submit