如何让Selenium 2.0等待页面加载?


当前回答

WebDriver driver = new ff / chrome / anyDriverYouWish(); .timeouts driver.manage()()。TimeUnit.SECONDS implicitlyWait(10日); 最多等待10秒。 WebDriverWait wait = new WebDriverWait(driver, 10); wait.until (ExpectedConditions。visibilityOf (WebElement元素)); FluentWait <司机> FluentWait; fluentWait = new fluentWait <>(driver)。TimeUnit.SECONDS withTimeout(30日) TimeUnit.MILLISECONDS .pollingEvery (200) . ignore (NoSuchElementException.class);

最后一个选项的优点是可以包含预期的异常,以便继续执行。

其他回答

/**
 * Call this method before an event that will change the page.
 */
private void beforePageLoad() {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.mpPageReloaded='notYet';");
}

/**
 * Call this method after an event that will change the page.
 * 
 * @see #beforePageLoad
 * 
 *      Waits for the previous page to disappear.
 */
private void afterPageLoad() throws Exception {
    (new WebDriverWait(driver, 10)).until(new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            Object obj = js.executeScript("return document.mpPageReloaded;");
            if (obj == null) {
                return true;
            }
            String str = (String) obj;
            if (!str.equals("notYet")) {
                return true;
            }
            return false;
        }
    });
}

在只更改部分文档的情况下,您可以从文档更改为元素。

这个技巧的灵感来自于sincebasic的答案。

你可以使用下面的代码片段来加载页面:

    IWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver,TimeSpan.FromSeconds(30.00));
    wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

或者你可以使用waiter的任何元素被加载,并成为可见/可点击的页面上,最有可能的是,这将是在加载结束时加载,如:

    Wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpathOfElement));
    var element = GlobalDriver.FindElement(By.XPath(xpathOfElement));
    var isSucceededed = element != null;

如果你想等待一个特定的元素加载,你可以在RenderedWebElement上使用isdisplay()方法:

// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
    // Browsers which render content (such as Firefox and IE) return "RenderedWebElements"
    RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className("gac_m"));

    // If results have been returned, the results are displayed in a drop down.
    if (resultsDiv.isDisplayed()) {
      break;
    }
}

(例子来自《5分钟入门指南》)

你可以使用等待。硒中基本上有两种类型的等待

隐式等 显式等

-隐式等待

这很简单,请参阅下面的语法:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

-显式等待

在此等待中显式等待或有条件等待,直到给定条件发生。

WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

您可以使用其他属性,如visblityOf(), visblityOfElement()

The best way to wait for page loads when using the Java bindings for WebDriver is to use the Page Object design pattern with PageFactory. This allows you to utilize the AjaxElementLocatorFactory which to put it simply acts as a global wait for all of your elements. It has limitations on elements such as drop-boxes or complex javascript transitions but it will drastically reduce the amount of code needed and speed up test times. A good example can be found in this blogpost. Basic understanding of Core Java is assumed.

http://startingwithseleniumwebdriver.blogspot.ro/2015/02/wait-in-page-factory.html