0

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?

4
  • Why "not sutiable" (its webdriver!) ? :-) And why not: stackoverflow.com/a/27203079/592355 ? Commented Apr 30, 2015 at 21:59
  • Because those answers offering to open a new empty tab while I want to open it with my URL. And, again, I'm still totally new with Selenium webdriver Commented Apr 30, 2015 at 22:09
  • So, you want to open a firefox browser -> Open a new tab -> enter a new URL and then navigate to it. Am I depicting the flow that you want to automate ? Or is it something different entirely ? Commented May 1, 2015 at 1:29
  • I already have Firefox browser window open. I want to open a new tab there. Well, I can open a new tab and after that insert there the URL and browse there, but I want to open a new tab from the beginning with that testing URL. Is it possible? Commented May 1, 2015 at 6:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

fingers xx'd it will port over easily and give you as great a result as i've been experiencing. all the best (and drop a little update with the java version for future visitors if it all pans out fine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.