【发布时间】:2015-08-04 21:41:21
【问题描述】:
在我的应用程序中单击“搜索”按钮会打开几个新窗口。 我需要找到其中一个并切换到它。
我用
driver.switchTo().window(popupHandle);
上述命令在 FF、Chrome、IE11 中运行良好,但在 IE8-10 中使用此命令时出现超时。虽然并非所有时间,超时都是随机的,但它们经常发生。
org.openqa.selenium.TimeoutException: Timed out waiting for page to load. (WARNING: The server did not provide any stacktrace information)
我已经研究和调试了两天这个问题,但找不到解决方法。 这是一个类似的问题,但对我没有帮助"Selenium WebDriver switching problem in IE"
这是我的测试用例的示例:
- 转到This page
- 填写城市、日期和至少 3 个(或全部,如果少于 3 个)网站进行比较
- 点击“搜索”
在某些测试用例中我需要切换到结果页面,在其他情况下切换到每个并获取标题。
这是我的代码示例:
public HotelsSearchResultsPage switchToHotelsSearchResultsPage() {
String currentSearchPageWindow = driver.getWindowHandle();
System.out.println("Current - " + currentSearchPageWindow);
Set<String> newOpenedWindows = driver.getWindowHandles();
System.out.println("All - " + newOpenedWindows);
Iterator<String> newOpenedWindow = newOpenedWindows.iterator();
while(newOpenedWindow.hasNext()) {
String popupHandle = newOpenedWindow.next().toString();
if(!popupHandle.contains(currentSearchPageWindow)) {
System.out.println("Switch to - " + popupHandle);
driver.switchTo().window(popupHandle);
if (driver.getTitle().contains("Search Results")) {
log.info("Found and switched to - " + driver.getTitle());
return PageFactory.initElements(driver, HotelsSearchResultsPage.class);
}
}
}
Assert.fail("Could not find 'FareCompare Search Results' page");
return null;
}
这是运行此测试用例几次后的日志:
Run1:
2015-05-22 13:19:59,387 INFO [class] Pushed 'Find Flights' button
Current - ba03b2c7-fe50-41f5-9109-6e68da460432
All - [12a3fe77-9181-4385-9682-d90733294f9f, 5c2c879e-5077-46b6-bfd6-323aea85c61d, ba03b2c7-fe50-41f5-9109-6e68da460432, 965c2650-c8d7-4262-bd79-c34bac78d163]
Switch to - 12a3fe77-9181-4385-9682-d90733294f9f
2015-05-22 13:20:14,561 INFO [class] Found and switched to - IEV LAX Flight Search Results - FareCompare
Run2:
2015-05-22 13:22:39,342 INFO [class] Pushed 'Find Flights' button
Current - 670bb987-c671-4e0f-9e0b-1f128154f567
All - [670bb987-c671-4e0f-9e0b-1f128154f567, 3428b6ba-a9b3-4480-9c82-ff75ecd3cbcd, 3d7c3894-8c1a-417b-a9f8-b9ba7f402331, a48e6b53-2f5a-4f19-a23f-3a7a705491fe]
Switch to - 3428b6ba-a9b3-4480-9c82-ff75ecd3cbcd
Switch to - 3d7c3894-8c1a-417b-a9f8-b9ba7f402331
2015-05-22 13:23:46,937 INFO [class] *** FAILURE
Run3:
2015-05-22 13:25:05,120 INFO [class] Pushed 'Find Flights' button
Current - 46028415-e337-4dbb-91bf-4cbfc71c98fe
All - [994e517c-0ac1-428c-8596-6aa2b0c3f9f0, 46028415-e337-4dbb-91bf-4cbfc71c98fe, 0b2b5867-012a-4b6e-9539-96258e18ce3b, 84905382-4d17-41af-9346-5a4fe444b4f9]
Switch to - 994e517c-0ac1-428c-8596-6aa2b0c3f9f0
Switch to - 0b2b5867-012a-4b6e-9539-96258e18ce3b
2015-05-22 13:26:16,501 INFO [class] *** FAILURE
Run4:
2015-05-22 13:13:30,109 INFO [class] Pushed 'Find Flights' button
Current - 142dd89b-99a8-4aa7-8e05-cb0377d72d24
All - [dd7935e7-e70f-48b6-8d9e-dd3185b02efa, 142dd89b-99a8-4aa7-8e05-cb0377d72d24, 0515e9e6-3a07-4edc-8008-5330d01f363e, 379ccfde-2c2b-45d8-bc5b-4f9638ec665d]
Switch to - dd7935e7-e70f-48b6-8d9e-dd3185b02efa
2015-05-22 13:14:36,543 INFO [class] *** FAILURE
如您所见,它只通过了一次。 任何想法如何使它工作? 谢谢
【问题讨论】:
-
相信你已经注意到,当你在IE中访问某些页面时,加载微调器有时会停留很长时间。现在的问题是 Selenium 在加载时不与浏览器通信,它只是等待(等待一段时间。之后它抛出提到的异常)。您可以尝试使用 Robot 或类似工具按下停止按钮,但我会做的是完全 reset IE,然后再试一次。
-
@skandigraun 感谢您的回复。不幸的是,我无法重置 IE,我使用的是 SauceLabs 虚拟机。当我尝试多次执行相同的步骤时,加载所有页面需要 IE 5-10 秒,所以看起来不是问题。
标签: java internet-explorer selenium selenium-webdriver webdriver