简体   繁体   中英

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?

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 (ie _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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM