[英]getWindowHandles method returns incorrect number of window handles using MS EDGE IE11 and Selenium version 4.1.2
Recently, I have upgraded my Selenium version from 2.53 to 4.1.2 to enable testing of our application on MS EDGE IE11.最近,我将我的 Selenium 版本从 2.53 升级到 4.1.2,以便能够在 MS EDGE IE11 上测试我们的应用程序。 But we are intermittently facing issues while retrieving number of windows open in MS EDGE IE11 with selenium-4.1.2
但是我们在使用 selenium-4.1.2 检索在 MS EDGE IE11 中打开的 windows 数量时间歇性地面临问题
Did anyone else facing similar kind of issues with Selenium-4.1.2?还有其他人遇到过与 Selenium-4.1.2 类似的问题吗?
Below is piece of code I have tried on MS EDGE IE11.下面是我在 MS EDGE IE11 上试过的一段代码。 Sometimes we could see its returning correct no.
有时我们可以看到它返回正确的号码。 of windows but sometime not.
windows 但有时不是。 We are also using sufficient wait-time before retrieving number of windows.
在检索号码 windows 之前,我们还使用了足够的等待时间。
Note - This is working absolutely fine on IE11 browser with Selenium-4.1.2注意 - 这在带有 Selenium-4.1.2 的 IE11 浏览器上工作得很好
int noOfWindowsOpen = driver.getWindowHandles().size();
Expectation: It should always return correct value of no.期望:它应该总是返回正确的值 no。 of windows open.
windows 打开。
Once you open the new tab / window before you count the number of WindowHandles you need to induce WebDriverWait for numberOfWindowsToBe()
as follows:打开新选项卡 / window后,在计算WindowHandles的数量之前,您需要为
numberOfWindowsToBe()
引入WebDriverWait ,如下所示:
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();
It seems to be a known limitation in automating Edge IE mode.这似乎是自动化 Edge IE 模式的一个已知限制。 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.
为确保新的 window 已成功创建并且 IEDriver 已检测到它,您必须不断检查Get Window Handles命令的结果,直到它包含新的 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();
}
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.执行之前,请确保ME Edge、IE和IE Driver的所有实例都已关闭。 If not, kill them forcefully from task manager and then re-run the Test Script.
如果没有,请从任务管理器中强制杀死它们,然后重新运行测试脚本。 Script will identify the new window properly.
脚本将正确识别新的 window。
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.