getWindowHandles method returns incorrect number of window handles using MS EDGE IE11 and Selenium version 4.1.2

512 views Asked by At

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.

3

There are 3 answers

0
Yu Zhou On

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();
}
1
undetected Selenium On

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();
0
Gaurank Verma On

I have found a work-around for above problem.

Before executing, make sure all the instances of ME Edge, IE and IE Driver are closed. If not, kill them forcefully from task manager and then re-run the Test Script. Script will identify the new window properly.

Thanks