0

Before tagging this as duplicate. Please read the Question. i have seen many answers for this kind of question. But none of them really worked. The closest that worked is this code below by twobytehero..

Selenium 2: Open link in new tab and close tabs

But its opening a window instead of a tab and i am not able to control the delay. I am using selenium 3. Using Selenium WebDriver with JAVA , I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). Whats the best possible way to do it with firefox ??

Community
  • 1
  • 1
  • clicking on a link should open a new tab with that url and i need to do certain operation and return back to parent. – Lijo Philip Nov 24 '16 at 12:22

1 Answers1

0

You can use approximately the same approach. For opening new tab use shortcut:

Ctrl + T

For navigating to previous tab:

Ctrl + Shift + Tab

For closing current tab:

Ctrl + W

Code snipped for opening new tab can look like following:

public static void openNewTab() {
    String openNewTabCombination = Keys.chord(Keys.CONTROL, "t");
    driver.get().findElement(By.tagName("body")).sendKeys(openNewTabCombination);
}

Also for moving to another tab with RemoteWebDriver you can use:

public static void moveToAnotherTab(RemoteWebDriver driver) {
    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }
}
catch23
  • 17,519
  • 42
  • 144
  • 217
  • My question is while clicking on WebElement elem = driver.findElement(By.xpath("//*[@id='confirmButtons'/a[1]")); elem.click(); it should open that link in a new tab. can you please tell how to achieve that? – Lijo Philip Nov 25 '16 at 08:23
  • @LijoPhilip Have you tried following - click by right mouse button and select open in the tab? – catch23 Nov 25 '16 at 15:06
  • Nope. How to do it? Can You help? – Lijo Philip Dec 01 '16 at 05:21