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?

Community
  • 1
  • 1
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Why "not sutiable" (its webdriver!) ? :-) And why not: http://stackoverflow.com/a/27203079/592355 ? – xerx593 Apr 30 '15 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 – Prophet Apr 30 '15 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 ? – Subh May 01 '15 at 01: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? – Prophet May 01 '15 at 06:12

1 Answers1

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.

jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • 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) – jim tollan May 01 '15 at 12:01