Home > Software engineering >  getWindowHandles method returns incorrect number of window handles using MS EDGE IE11 and Selenium v
getWindowHandles method returns incorrect number of window handles using MS EDGE IE11 and Selenium v

Time:04-08

Recently, I have upgraded my Selenium version from 2.53 to 4.1.2 to enable testing of our application on MS EDGE IE11. But we are intermittently facing issues while retrieving number of windows open in MS EDGE IE11 with selenium-4.1.2

Did anyone else facing similar kind of issues with Selenium-4.1.2 ?

Below is piece of code I have tried on MS EDGE IE11. Sometimes we could see its returning correct no. of windows but sometime not. We are also using sufficient wait-time before retrieving number of windows.

Note - This is working absolutely fine on IE11 browser with Selenium-4.1.2

int noOfWindowsOpen = driver.getWindowHandles().size();

Expectation : It should always return correct value of no. of windows open.

CodePudding user response:

Once you open the new tab / window before you count the number of WindowHandles you need to induce WebDriverWait for numberOfWindowsToBe() as follows:

driver.get("http://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
int noOfWindowsOpen = driver.getWindowHandles().size();

CodePudding user response:

It seems to be a known limitation in automating Edge IE mode. It says:

To ensure the new window has been created successfully and IEDriver has detected it, you must continuously check the result of the Get Window Handles command until it contains a handle to the new window.

You can try the sample code in it:

int initialHandleCount = driver.getWindowHandles().size();
driver.findElement(By.id("<Id of the button that will open a new window>")).click();        
Set<string> newHandles = driver.getWindowHandles();
while (newHandles.size() == initialHandleCount) {
    newHandles = driver.getWindowHandles();
}
  • Related