How do you get selenium to recognize that a page l

2020-02-10 05:02发布

In certain unknown situations selenium does not detect that a page has loaded when using the open method. I am using the Java API. For example (This code will not produce this error. I don't know of an externally visible page that will.):

Selenium browser = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
browser.start();
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");

When the error occurs, the call to 'open' times out, even though you can clearly see that the page has loaded successfully before the timeout occurs. Increasing the timeout does not help. The call to 'type' never occurs, no progress is made.

How do you get selenium to recognize that the page has loaded when this error occurs?

标签: java selenium
9条回答
Viruses.
2楼-- · 2020-02-10 05:43

I faced this problem quite recently.

All JS-based solutions didn't quite fit ICEFaces 2.x + Selenium 2.x/Webdriver combination I have.

What I did and what worked for me is the following:

In the corner of the screen, there's connection activity indicator.

            <ice:outputConnectionStatus id="connectStat"
                                        showPopupOnDisconnect="true"/>

In my Java unit test, I wait until its 'idle' image comes back again:

private void waitForAjax() throws InterruptedException {
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { 
            if ("visibility: visible;".equals(
                selenium.getAttribute("top_right_form:connectStat:connection-idle@style"))) { 
                break;
            }
        } catch (Exception e) {

        }
        Thread.sleep(1000);
    }
}

You can disable rendering of this indicator in production build, if showing it at the page is unnecessary, or use empty 1x1 gifs as its images.

Works 100% (with popups, pushed messages etc.) and relieves you from the hell of specifying waitForElement(...) for each element separately.

Hope this helps someone.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-10 05:44

If you page has no AJAX, try to seek footer of page (I also use Junit fail(""), you may use System.err.println() instead):

    element.click();
    int timeout =120; 
    // one loop = 0.5 sec, co it will be one minute 
    WebElement myFooter = null;

    for(int i=0; i<timeout; i++){
      myFooter = driver.findElement(By.id("footer"));
      if(myFooter!= null){
        break;
      }
      else{
        timeout--;
      }
}
if(timeout==0 && myFooter == null){
  fail("ERROR! PAGE TIMEOUT");
}
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-10 05:47

When I do Selenium testing, I wait to see if a certain element is visible (waitForVisible), then I do my action. I usually try to use an element after the one I'm typing in.

查看更多
登录 后发表回答