本文介绍了你怎么得到selenium来识别一个页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些未知情况下,selenium在使用open方法时未检测到页面已加载。我正在使用Java API。例如(此代码不会产生此错误。我不知道会有外部可见的页面。):

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");

当错误发生时,打开的调用超时,即使你可以清楚地看到在超时发生之前,页面已成功加载。增加超时没有帮助。对'type'的调用永远不会发生,没有进展。

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.

当发生此错误时,如何让selenium识别页面已加载?

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

推荐答案

也许这会对你有帮助....

Maybe this will help you....

考虑以下方法是在名为Functions.java的页面中

Consider the following method is in page called Functions.java

public static void waitForPageLoaded(WebDriver driver) {

         ExpectedCondition<Boolean> expectation = new
    ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
              return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
          };

          WebDriverWait wait = new WebDriverWait(driver,30);
          try {
                  wait.until(expectation);
          } catch(Throwable error) {
                  Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
          }
     }

您可以将此方法调用到您的函数中。由于它是静态方法,您可以直接使用类名调用。

And you can call this method into your function. Since it is a static method, you can directly call with the class name.

public class Test(){
    WebDriver driver;

    @Test
    public void testing(){
         driver = new FirefoxDriver();
         driver.get("http://www.gmail.com");
         Functions.waitForPageLoaded(driver);
   }
}

这篇关于你怎么得到selenium来识别一个页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:55
查看更多