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?
-
Why "not sutiable" (its webdriver!) ? :-) And why not: stackoverflow.com/a/27203079/592355 ?xerx593– xerx5932015-04-30 21:59:39 +00:00Commented 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 webdriverProphet– Prophet2015-04-30 22:09:16 +00:00Commented 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 ?Subh– Subh2015-05-01 01:29:29 +00:00Commented 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?Prophet– Prophet2015-05-01 06:12:03 +00:00Commented May 1, 2015 at 6:12
Add a comment
|
1 Answer
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.
1 Comment
jim tollan
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)