Switch Tabs Using Selenium Webdriver with Java

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.

Switch between browser tabs using Selenium WebDriver with Java

You have to use window handle function here. Here is a sample working code in java:

    String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window

Hope this helps!

How to switch tab using for loop in Selenium Webdriver

You need to switch back to the original window where the links are.

  1. Click on one if the links
  2. Switch to that tab
  3. Close tab
  4. Switch back to default window where you are getting the links

Easiest way is to store the "original" window and switch back to that one all the time.
winHandleBefore = driver.getWindowHandle();

when closing the tab

driver.close(); 

driver.switchTo().window(winHandleBefore)

How to open a new tab using Selenium WebDriver in Java?

The code below will open the link in a new tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

The code below will open an empty new tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

Switching to elements of new tab - selenium

I have found another solution, which is working for me, - using windows handlers

String parentWindowHandler=driver.getWindowHandle();// Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window

and you can switch back to parent window when required by
driver.switchTo().window(parentWindowHandler);

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


Related Topics



Leave a reply



Submit