Gordon DePuy
Gordon DePuy

Reputation: 11

Selenium web driver | java | unable to 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();
driver.get("http://mail.google.com");

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

driver = new FirefoxDriver();
driver.get("http://www.google.com");

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

// It fails right here!
driver.switchTo().window(firstWindowHandle );
driver.get("http://www.lifehacker.com");

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.

Upvotes: 0

Views: 3352

Answers (2)

Vikram Aditya
Vikram Aditya

Reputation: 1

Swtiching between 2 active Windows:

FirefoxDriver wd=new FirefoxDriver();
    wd.get("https://irctc.co.in/");

    wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);

    WebElement wb=wd.findElement(By.linkText("Cabs"));
    wb.click();  //Now 2 Windows are open

    wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS); //Wait for the complete page to load

    Set<String> sid=wd.getWindowHandles(); //getWindowHandles() method returns the ids of all active Windows and its return type will be a Collection Set.

    Iterator<String> it=sid.iterator(); //Using iterator we can fetch the values from Set.

String parentId=it.next();
System.out.println(parentId);
String childId=it.next();
System.out.println(childId);

wd.switchTo().window(childId);  //swtiching control to child Window

wd.close(); //control returns to the parent Window.

Upvotes: 0

Anders
Anders

Reputation: 15397

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:

window.open('http://www.bing.com','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();
driver.get("http://mail.google.com");

FirefoxDriver driver2 = new FirefoxDriver();
driver2.get("http://www.google.com");

Upvotes: 1

Related Questions