Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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));
}
share|improve this question

3 Answers 3

up vote 5 down vote accepted

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);

share|improve this answer
    
Wow, clever clever!! I should have thought of that!! –  MacGyver Mar 12 '12 at 16:39
    
Ah, pretty cool. –  Sam Woods Mar 12 '12 at 17:55
    
Glad it helped :) –  SteveCZ Mar 12 '12 at 21:12
    
What's the proper import to use IJavaScriptExecutor? –  Burton Kent Oct 10 '12 at 0:02

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");

share|improve this answer
    
haha! Damn, I was hoping that wasn't the answer. :-) It takes like 10-15 seconds to launch a new instance of WebDriver. –  MacGyver Feb 24 '12 at 23:37

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")))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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