我正在尝试使用IE上的selenium web驱动程序切换到弹出窗口。当我到达切换到弹出窗口的行时,代码就会挂起。我正在寻找各种方法,我可以尝试将窗口切换10次并在20秒后尝试另一次尝试,因为我尝试在下面进行操作或更好的方法来确保窗口正常切换。如果我手动关闭弹出窗口,我会得到noSuchWindow Exceptions并且代码会弹出来。在发布此文章之前,我已经审核了其他stackoverflow文章,但我相信我的问题是独一无二的。这是我的情景:
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"));
CI信息:
JDK:1.8.0_66
Java:版本8
IE:11
其他类似的问题没有回答我的问题:
switchWindow does not work in IE
How to switch to the new browser window, which opens after click on the button?
How to exit a while loop after a certain time?
非常感谢任何帮助或反馈!
答案 0 :(得分:0)
使用以下代码,我针对我的私人代码和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));