Amit Asaf
Amit Asaf

Reputation: 175

Open several URL in several New Tab on same session in Selenium

I need to navigate to several urls (via Firefox). I want to open each url in a different tab (but not in a different window). I've read many similar questions on SO - the only suggestion that seems to work some of the time is:

((JavascriptExecutor) driver).executeScript("window.open(arguments[0])", url);

However this does not work all the time - sometimes it opens the link in a new window instead of new tab.

I thought it might have to do with the specific Firefox configuration I'm using but changing value for browser.link.open_newwindow.override.external (to 3) does not seem to resolve the issue.

For extra clarity, I'm not talking about opening a link on a web page in new tab (I know this is discussed in other threads here on stackoverflow) I'm talking about directly navigating to several urls in new tab.

So how can I get Firefox to open urls in new tab?

Upvotes: 2

Views: 593

Answers (1)

Mahmud Riad
Mahmud Riad

Reputation: 1165

The exact java solution for openning new tab in current session is below

((JavascriptExecutor)driver).executeScript("window.open('http://www.amazon.com');");

It will automatic switch to new tabs handle. If you want to switch between tabs driver handle please implement this.

String mainWindowHandler = driver.getWindowHandle();

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

EDIT : If you use url as a variable then use the below code

 String url = "http://www.amazon.com/";
((JavascriptExecutor)driver).executeScript("window.open('" + url + "');");  

Upvotes: 2

Related Questions