IE问题中的Selenium SwitchWindow



我正在尝试在IE上使用Selenium Web驱动程序切换到弹出窗口。当我到达切换到弹出窗口的行时,代码就会挂起。我正在寻找可以尝试切换窗口 10 次并在 20 秒后尝试再次尝试的方法,就像我在下面尝试的那样,或者更好的方法来确保窗口正确切换。如果我手动关闭弹出窗口,我会得到没有SuchWindow异常并且代码会爆炸出来。 在发布此文章之前,我已经查看了其他堆栈溢出文章,但我相信我的问题很独特。这是我的方案:

Scenario:
1. Retrieve parent window handle
2. Perform action launching popup window
3. Get the popup window handles and store them in a string set
4. Loop through window handles until there are no more. Retrieve Popup window handle
5. Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
6. Switch to popup ---Code Hangs Here---
7. Retrieve popup title
8. Close popup
9. Switch to parent
10. Verification of title

以下是与上述方案相关的所有代码:

String popupWindow = "";
String parentWindow = "";
int saveCount = 0;
int getPopupCount = 0;
int tryCount = 0;
// Get Parent window handle
parentWindow = driver.getWindowHandle();
System.out.println("parentWindow: " + parentWindow);
Thread.sleep(500);
//Perform Action launching popup window
//Get the popup window handles and store them in a string set
Set<String> popups =  driver.getWindowHandles();
saveCount = getPopupCount;
try {
tryCount = 0;
//Loop until the popup count does not equal the save count or until 10 tries (20 seconds) have passed
while (saveCount == getPopupCount && tryCount++ < 10) {
//Wait 2 second
Thread.sleep(2000);
getPopupCount = popups.size();
System.out.println("getPopupCount: -" + getPopupCount + "-");
}//end while
if (tryCount >= 10) {
System.out.println("Failed after 10 tries");
}//end if
//Loop through window handles until there are no more. Retrieve Popup window handle
Iterator<String> myIterator = popups.iterator();
while (myIterator.hasNext()) {
popupWindow = myIterator.next();
System.out.println("popupWindow: " + popupWindow);
System.out.println("Boolean should be false: " + parentWindow.equalsIgnoreCase(popupWindow));
Thread.sleep(5000);
//fetch starting time
long startTime = System.currentTimeMillis(); 
//Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
while(!parentWindow.equalsIgnoreCase(popupWindow) && (System.currentTimeMillis()-startTime) < 20000) {
try{
Thread.sleep(500);
//Switch to the Popup window            
//TODO - This is where it fails
driver.switchTo().window(popupWindow);
Thread.sleep(500);
System.out.println(driver.getTitle());
popupTitle = driver.getTitle();
//Close the Popup Window
driver.close();
} catch (Exception e) {
throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
+ "...Error: " + e.getMessage());
}//end catch
}//end if
}//end while
} catch(Exception e) {
throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
+ "...Error switching to and closing popup: " + e.getMessage());
}//end catch
//Switch to parent window.
driver.switchTo().window(parentWindow);
driver.manage().window().maximize();
Thread.sleep(2000);
//Verification of title
Assert.assertTrue(popupTitle.contains("MYTITLE"));

孔子学院信息:

JDK:1.8.0_66

爪哇:版本 8

IE:11

其他没有回答我问题的类似问题:

开关窗口在IE中不起作用

如何切换到单击按钮后打开的新浏览器窗口?

如何在一定时间后退出 while 循环?

任何帮助或反馈将不胜感激!

使用以下代码,我针对我的私有代码和 http://demo.guru99.com/popup.php 演示弹出站点进行了测试。我的代码在该站点上运行良好,但在我的专用站点上失败。我实现了等待期,但我不认为这是一个时间问题。我只是认为弹出窗口在我的私人网站上与 Selenium 在 IE 上不兼容。发布我在虚拟网站上工作的代码作为答案,以防其他人遇到类似的问题,因为代码是有效的。

//Retrieve parent window handle
parentWindow = driver.getWindowHandle();        
//Loop through the window handles until you are on the popup window
for(String popupWindow : driver.getWindowHandles()){
if (driver.switchTo().window(popupWindow).getTitle().equals(myTitle)) {
break;
} 
else {
driver.switchTo().window(parentWindow);
} 
}
//Store the title
popupTitle = driver.getTitle();
//Close the Popup Window
driver.close();     
//switch to parent window.
driver.switchTo().window(parentWindow);
driver.manage().window().maximize();
//Verification of title
Assert.assertTrue(popupTitle.toUpperCase().contains(myTitle));

最新更新