Selenium Web Driver | java | can not switch between Firefox browser windows


Using Selenium Webdriver 2. java.

I would like to switch back in forth between two firefox browser windows. When I do I get: org.openqa.selenium.NoSuchWindoException: Unable to loacate window"{accb1cc2-74c9-3b4e-8f71-c0b184a037c4}"; duration or timeout:

Here is the java:

driver = new FirefoxDriver();

String firstWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+firstWindowHandle);

driver = new FirefoxDriver();

// Get names of currently open windows
String secondWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+secondWindowHandle);

// It fails right here!
driver.switchTo().window(firstWindowHandle );

It prints the following to the console: - handle of first window = {accb1cc2-74c9-3b4e-8f71-c0b184a037c4} - handle of the second window = {f5256619-a36e-a441-9979-937da0abacd1}

All help is appreciated.

Unfortunately, you cannot switch between windows the way you are currently trying to do it - WebDriver lost the first window as soon as you instantiated a new instance.

You could try opening the second window via javascript and then switching back and forth from it:'','Bing','modal=yes,alwaysRaised=yes')

This is a bit of a hack, and could have the following problems:

  • Popup blockers may prevent the action
  • The browser must have javascript enabled
  • Future browser versions may break the hack
  • Complaining and murmuring from peers (and perhaps rightly so) because even though it might work, it's still a hack ;)

Some final thoughts:

Is there any particular reason it has to be the same driver instance? If not, just switch between two driver instances:

FirefoxDriver driver = new FirefoxDriver();

FirefoxDriver driver2 = new FirefoxDriver();