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条回答
萌系小妹纸
2楼-- · 2020-02-10 05:25

I've run into similar issues when using Selenium to test an application with iFrames. Basically, it seemed that once the primary page (the page containing the iframes) was loaded, Selenium was unable to determine when the iframe content had finished loading.

From looking at the source for the link you're trying to load, it looks like there's some Javascript that's creating additional page elements once the page has loaded. I can't be sure, but it's possible that this is what's causing the problem since it seems similar to the situation that I've encountered above.

Do you get the same sort of errors loading a static page? (ie, something with straight html)

If you're unable to get a better answer, try the selenium forums, they're usually quite active and the Selenium devs do respond to good questions.

http://clearspace.openqa.org/community/selenium_remote_control

Also, if you haven't already tried it, add a call to browser.WaitForPageToLoad("15000") after the call to open. I've found that doing this after every page transition makes my tests a little more solid, even though it shouldn't technically be required. (When Selenium detects that the page actually has loaded, it continues, so the actual timeout variable isn't really a concern..

查看更多
我只想做你的唯一
3楼-- · 2020-02-10 05:25

Not a perfect solution, but I am using this method

$t1 = time(); // current timestamp
$this->selenium->waitForPageToLoad(30);
$t2 = time();

if ($t2 - $t1 >= 28) {
    // page was not loaded
}

So, it is kind of checking if the page was not loaded during the specified time, so it is not loaded.

查看更多
疯言疯语
4楼-- · 2020-02-10 05:27

Using 'openAndWait' in place of 'open' will do the trick.

From the website:

Many Actions can be called with the "AndWait" suffix, e.g. "clickAndWait". This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load.

查看更多
放荡不羁爱自由
5楼-- · 2020-02-10 05:31

Maybe this will help you....

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);
   }
}
查看更多
够拽才男人
6楼-- · 2020-02-10 05:35

another idea is to modify AJAX API (to add some text after AJAX actions). After ajax action was finished, before return, set invisible field to TRUE, selenium will find it and read as green-light

in html:

<input type='hidden' id="greenlight">

in selenium

if(driver.findElement(By.id("greenlight")).getAttr("value").equals("TRUE")){
    // do something after page loading
}
查看更多
神经病院院长
7楼-- · 2020-02-10 05:41

Enabling the 'multiWindow' feature solved the issue, though I am not clear why.

SeleniumServer(int port, boolean slowResources, boolean multiWindow)

SeleniumServer server = new SeleniumServer(4444, false, true);

Any clarification would be helpful.

查看更多
登录 后发表回答