Prophet
Prophet

Reputation: 33361

How to open a web page in a new tab with Selenium WebDriver

I'm new to Selenium.
I use Java language.
I want to open some web page, say http://google.com in a new tab. driver.get("http://google.com"); works OK but opens it in a new window.
I don't want to open an empty new tab, I want to open a new tab with an URL I want (http://google.com)
I went through answers here How to open a new tab using Selenium WebDriver with Java? but didn't find suitable, working for me solution.
Is it possible?

Upvotes: 0

Views: 1762

Answers (1)

jim tollan
jim tollan

Reputation: 22485

Potentially, you'll be able to port this over to Java. This is an extension method that I created a while back for use in c#. Basically, it uses local javascript to open the new tab in the target browser (i.e. _driver):

public static void OpenTab(this IWebDriver driver, string url)
{
    var windowHandles = driver.WindowHandles;
    var script = string.Format("window.open('{0}', '_blank');", url);
    ((IJavaScriptExecutor)driver).ExecuteScript(script);
    var newWindowHandles = driver.WindowHandles;
    var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
    driver.SwitchTo().Window(openedWindowHandle);
}

usage:

var url = "http://google.com";
_driver.OpenTab(url);

give it a wee spin and see if you can at least grok the methodology at play.

Upvotes: 2

Related Questions