在Webdriver中提交表单后,如何等待页面加载。我正在使用firefox驱动程序

时间:2011-05-30 01:08:10

标签: webdriver

我正在尝试自动化测试用例,我通过点击图像提交表单。

页面重新加载后,我无法与网页上的任何元素进行交互

我正在使用java,firefox驱动程序。

代码卡住了,根本无法识别元素。

有没有像QTP,selenium那样的webdriver的等待机制?

5 个答案:

答案 0 :(得分:4)

2年后,ruby实施:

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.util {
  @driver.execute_script("return document.readyState;") == "complete" 
}

答案 1 :(得分:3)

只需使用FluentWait类:

/**
 * An implementation of the {@link Wait} interface that may have its timeout
 * and polling interval configured on the fly.
 *
 * <p>Each FluentWait instance defines the maximum amount of time to wait for
 * a condition, as well as the frequency with which to check the condition.
 * Furthermore, the user may configure the wait to ignore specific types of
 * exceptions whilst waiting, such as
 * {@link org.openqa.selenium.NoSuchElementException NoSuchElementExceptions}
 * when searching for an element on the page.
 *
 * <p>Sample usage:
 * <code><pre>
 *   // Waiting 30 seconds for an element to be present on the page, checking
 *   // for its presence once every 5 seconds.
 *   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
 *       .withTimeout(30, SECONDS)
 *       .pollingEvery(5, SECONDS)
 *       .ignoring(NoSuchElementException.class);
 *
 *   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
 *     public WebElement apply(WebDriver driver) {
 *       return driver.findElement(By.id("foo"));
 *     }
 *   });
 *

WebDriverWait

答案 2 :(得分:0)

我认为使用selenium 2,您无需在使用Firefox webdriver提交表单后等待。

     element.sendKeys("34344343");
     webDriver.findElement(By.id("searchSubmitButton")).click();

WebElement columnInPostedPage =  webDriver.findElement(By.xpath("//div[@id='content']/table/tbody/tr[2]/td[3]")); //

如果在加载页面后通过javascript加载内容,则可以对内容执行类似的操作

     query.submit();

    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        WebElement result = webDriver.findElement(By.id("content"));
        if (result.isDisplayed()) {
          break;
        }
        //Thread.sleep(1000);
    }        

     WebElement columnInPostedPage =  webDriver.findElement(By.xpath("//div[@id='content']/table/tbody/tr[2]/td[3]")); //

答案 3 :(得分:-1)

for (int second = 0;; second++) {
            if (second >= 60) fail("timeout");
            try { if (isElementPresent(By.linkText("element"))) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }

答案 4 :(得分:-2)