0

In my application clicking on "Search" button opens few new windows. I need to find one of them and switch to it.

I use

driver.switchTo().window(popupHandle);

Command above works fine in FF, Chrome, IE11, but i'm getting timeouts when use this command in IE8-10. Not all the time though, timeouts are random but they happen often.

org.openqa.selenium.TimeoutException: Timed out waiting for page to load. (WARNING: The server did not provide any stacktrace information)

I was researching and debugging this issue for two days already, but couldn't find fix. Here is a similar question, but it didn't help me "Selenium WebDriver switching problem in IE"

Here is an example of my test case:

  1. Go to This page
  2. Fill up City, Dates and minimum 3 (or all if less than 3) sites to compare
  3. Click "Search"

In some test cases I need to switch to results page, in other switch to each and get titles.

Here si my code example:

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;
}

Here is a log after running this test case few times:

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

As you see, it passed only one time. Any ideas how to make it work? Thanks

Community
  • 1
  • 1
Dmitry Shyshkin
  • 384
  • 2
  • 8
  • I'm sure that you have noticed that when you visit some pages in IE, sometimes the loading spinner will stay there for a very long time. Now the problem is that Selenium does not communicate with browsers while they are loading, it just waits (for a while. After that it throws the mentioned exception). You could try to press the stop button with Robot or similar, but what I would do is to [reset IE](https://support.microsoft.com/en-us/kb/923737) completely, and try again. – skandigraun May 23 '15 at 01:43
  • @skandigraun thanks for reply. Unfortunatelly, I can't reset IE, I use SauceLabs VMs. And when I try to do same steps manyally, it takes IE 5-10 seconds to load all pages, so it doesn't look like its a problem. – Dmitry Shyshkin May 28 '15 at 15:10

1 Answers1

0

In the past I have found that when there is any problems switching windows, using a JavascriptExecutor to switch a window is a workaround that you could use.

djangofan
  • 28,471
  • 61
  • 196
  • 289