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

9

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

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

我正在使用Java,Firefox驱动程序。

代码被卡住了,完全无法识别元素。

是否有类似于QTP,Selenium中的WebDriver等待机制?

5个回答

5

两年后,Ruby 实现:

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

很酷。但是当前版本在处理页面转换加载方面表现不佳! - Thiago Macedo
1
@ThiagoFMacedo 我不了解 Linux,但在 Windows 上它的运行效果并不好。我们采用了类似于 allenhwkim 建议的做法。 - Peter Vrabel
那里有一个错别字。应该是wait.until,而不是util - labyrinth

3
只需使用FluentWait类: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.39(C#绑定的版本),但似乎没有这个类。这个类是从哪个dll中来的?也许我漏掉了其中一个。 - JonnyRaa

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

Selenium 2 是否会在 click() 后等待取决于页面加载方式。 - reinierpost
我认为异常会将你从时间循环中抛出。我不认为这会起作用。你需要使用带有.ignoring方法的WebDriverWait。 - djangofan

-2

我猜waitForPageLoad是在使用selenium RC时使用的。那么Webdriver呢?我正在使用Firefox driver....我进行了一些尝试和错误,似乎在提交表单时不需要编写任何内容。有人可以评论一下吗? - SK176H
请参见https://dev59.com/hWgv5IYBdhLWcg3wSewe。 - reinierpost

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

2
-1:Webdriver有其隐式和显式等待的方法:此处 - Alex Okrushko
谢谢回答。将来会学习更多关于Selenium的知识。 - Marouane Gazanayi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接