Selenium Switch Tabs

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.

How to open a new tab using Selenium WebDriver in C#?

Selenium 4 solution:

driver.SwitchTo().NewWindow(WindowType.Tab);

Note that it will open a new tab in the same window and will switch also to the newly opened tab.

to open a new window, you should use:

driver.SwitchTo().NewWindow(WindowType.Window);

Selenium 3 solution:

((IJavaScriptExecutor)driver).ExecuteScript("window.open()");
List<string> tabs = new List<string> (driver.WindowHandles);
driver.SwitchTo().Window(tabs[1]);

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

Windows vs tabs in Selenium

With the terminology used to distinguish between driver.close() and driver.quit() methods supported by Selenium WebDriver in the post you are referencing to, they actually mostly mean browser tabs, not windows.

By opening a new browser window with

((JavascriptExecutor)driver).executeScript("window.open('')");

or by clicking on some web element opening a new browser tab/window is will normally open a new tab in the existing browser.

How to open multiple tabs and switch between through Selenium and Webdriver?

Here is the sample example to open multiple tabs and switch between them through Selenium Webdriver:

  • Code Block:

    import java.util.Set;

    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class WINDOW_HANDLE_ITERATE_Firefox
    {
    public static void main(String[] args) throws Exception
    {
    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");
    String parent_window = driver.getWindowHandle();
    System.out.println("Parent Window Handle is: "+driver.getWindowHandle());
    System.out.println("Page Title is: "+driver.getTitle());
    ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
    new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> allWindows_1 = driver.getWindowHandles();
    System.out.println("Total Windows: "+allWindows_1.size());
    for(String hand1:allWindows_1)
    if(!parent_window.equals(hand1))
    {
    driver.switchTo().window(hand1);
    new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook"));
    String first_child_window = driver.getWindowHandle();
    System.out.println("First Child Window Handle is: "+first_child_window);
    System.out.println("First Child Window Page Title is: "+driver.getTitle());
    driver.close();
    }
    driver.switchTo().window(parent_window);
    System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" );
    driver.quit();
    }
    }
  • Console Output:

    INFO: Detected dialect: W3C
    Parent Window Handle is: 6442450945
    Page Title is: Google
    Total Windows: 2
    First Child Window Handle is: 6442450949
    First Child Window Page Title is: Facebook – log in or sign up
    Current Window Handle is : 6442450945 which is same as 6442450945, which is the parent window handle


Related Topics



Leave a reply



Submit