5

How do I open a new browser window in Selenium WebDriver? I'm using C# client drivers, but if you know with any language, I should be able to find the correct class/method in C#. On Stack Overflow, I read it's not possible to open a new tab, so I won't ask that.

For example, this will navigate to a new URL in the same window:

public static void DoNavigate(IWebDriver driver, string url)
{
    driver.Navigate().GoToUrl(url);  
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(Constants.WAIT_TIME));
}

4 Answers 4

9

It is not necessary to create a new instance of the driver object. You can use JavaScript to open a new window which is a lot faster.

To open the window:

IJavaScriptExecutor jscript = driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open()");

Then to switch windows, use the window handles:

List<string> handles = driver.WindowHandles.ToList<string>();
driver.SwitchTo().Window(handles.Last());

driver.Navigate().GoToUrl(url);
5
  • Wow, clever clever!! I should have thought of that!! Mar 12, 2012 at 16:39
  • Ah, pretty cool.
    – Sam Woods
    Mar 12, 2012 at 17:55
  • Glad it helped :)
    – SteveCZ
    Mar 12, 2012 at 21:12
  • What's the proper import to use IJavaScriptExecutor? Oct 10, 2012 at 0:02
  • I highly doubt the tabs are in order. Both the C# and Java APIs return a non-ordered collection. The Javascript API says it returns a list but there is no mention that the list is ordered with the newest tab last. In short: I don't think you can assume the list will be in any particular order, nor that the order will remain the same over time.
    – Gili
    Nov 19, 2014 at 6:01
1

You have to create a new instance of the driver object. This will open a new window and you can switch between windows by using the different driver objects. For example:

IWebDriver driver = new FirefoxDriver();

DoNavigate(driver, "http://google.com");

1
  • haha! Damn, I was hoping that wasn't the answer. :-) It takes like 10-15 seconds to launch a new instance of WebDriver. Feb 24, 2012 at 23:37
1

Here is an example in Clojure:

(import [org.openqa.selenium.firefox FirefoxDriver])

(let [driver1 (FirefoxDriver.) 
      driver2 (FirefoxDriver.)]
  (.get driver1 "http://www.google.com")
  (.get driver2 "http://sqa.stackexchange.com/q/2701/246")))
0

Here's how I'm doing it in VB with FirefoxDriver. I created a wrapper for the webdrivers, but without a wrapper it looks something like this. IWebDriver has two handle controls; CurrentWindowHandle and a collection of WindowHandles. The handles in WindowHandles are in the order in which they were created, so getting the last handle in the list gets the window you just opened. In my experience CurrentWindowHandle is NOT the new window, but the window in which you executed the javascript command.

To open a new window:

Dim script As String = "window.open()"
Dim jScript As IJavaScriptExecutor = CType(driver, IJavaScriptExecutor).ExecuteScript(script)

To switch to the most recently opened window:

Dim lastWindowOpened As Integer = driver.WindowHandles.count - 1
driver.SwitchTo().Window(driver.WindowHandles(lastWindowOpened))

Close or otherwise do something to each handle:

For Each window In driver.WindowHandles
   driver.SwitchTo().Window(window)
   driver.Close()
Next

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.