等待Selenium中的页面加载

时间:2011-05-03 10:59:15

标签: java selenium webdriver selenium-webdriver

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

48 个答案:

答案 0 :(得分:124)

您还可以使用以下代码检查网页加载

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));

 wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

答案 1 :(得分:85)

使用课程WebDriverWait

另见here

你可以期待展示一些元素。类似于C#:

WebDriver _driver = new WebDriver();
WebDriverWait _wait = new WebDriverWait(_driver, new TimeSpan(0, 1, 0));

_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));

答案 2 :(得分:32)

如果您设置驱动程序的隐式等待,然后在您希望在加载页面上的元素上调用findElement方法,WebDriver将轮询该元素,直到找到该元素或达到时间超出价值。

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

来源:implicit-waits

答案 3 :(得分:32)

通常,使用Selenium 2.0时,Web驱动程序只应在确定页面已加载后才将控制权返回给调用代码。如果没有,您可以调用waitforelemement,它会绕着调用findelement循环,直到找到它或超时(超时可以设置)。

答案 4 :(得分:21)

Ruby实现:

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

答案 5 :(得分:17)

您可以删除System.out行。它是为了调试目的而添加的。

WebDriver driver_;

public void waitForPageLoad() {

    Wait<WebDriver> wait = new WebDriverWait(driver_, 30);
    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            System.out.println("Current Window State       : "
                + String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")));
            return String
                .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                .equals("complete");
        }
    });
}

答案 6 :(得分:11)

您还可以使用类: ExpectedConditions 明确等待元素显示在网页上,然后才能采取任何操作进一步操作

您可以使用ExpectedConditions类来确定元素是否可见:

WebElement element = (new WebDriverWait(getDriver(), 10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#houseName")));

有关您可以检查的所有条件的列表,请参阅ExpectedConditions class Javadoc

答案 7 :(得分:11)

所有这些解决方案都适用于特定情况,但它们至少会遇到一些可能的问题:

  1. 它们不够通用 - 它们希望您提前知道某些特定情况对于您要访问的页面是否正确(例如,将显示某个元素)

    < / LI>
  2. 他们对竞争条件持开放态度,您可以使用旧页面上实际存在的元素以及新页面。

  3. 这是我尝试避免此问题的通用解决方案(在Python中):

    首先,一个通用的“等待”功能(如果你愿意,可以使用WebDriverWait,我发现它们很难看):

    def wait_for(condition_function):
        start_time = time.time()
        while time.time() < start_time + 3:
            if condition_function():
                return True
            else:
                time.sleep(0.1)
        raise Exception('Timeout waiting for {}'.format(condition_function.__name__))
    

    接下来,该解决方案依赖于selenium为页面上的所有元素记录(内部)id号的事实,包括顶级<html>元素。当页面刷新或加载时,它会获得一个带有新ID的新html元素。

    因此,假设您要点击带有“我的链接”文本的链接,例如:

    old_page = browser.find_element_by_tag_name('html')
    
    browser.find_element_by_link_text('my link').click()
    
    def page_has_loaded():
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    
    wait_for(page_has_loaded)
    

    对于更多Pythonic,可重用的通用助手,您可以创建一个上下文管理器:

    from contextlib import contextmanager
    
    @contextmanager
    def wait_for_page_load(browser):
        old_page = browser.find_element_by_tag_name('html')
    
        yield
    
        def page_has_loaded():
            new_page = browser.find_element_by_tag_name('html')
            return new_page.id != old_page.id
    
        wait_for(page_has_loaded)
    

    然后你可以在几乎所有的硒交互中使用它:

    with wait_for_page_load(browser):
        browser.find_element_by_link_text('my link').click()
    

    我认为那是防弹的!你怎么看?

    blog post about it here

    中的更多信息

答案 8 :(得分:9)

如果您要等待加载特定元素,可以在isDisplayed()上使用RenderedWebElement方法:

// 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;
    }
}

(来自The 5 Minute Getting Started Guide的例子)

答案 9 :(得分:9)

这似乎是WebDriver的严重限制。显然等待一个元素并不意味着页面被加载,特别是DOM可以完全构建(onready状态),JS仍在执行,CSS和图像仍在加载。

我认为最简单的解决方案是在初始化所有内容之后在onload事件上设置一个JS变量,并在Selenium中检查并等待这个JS变量。

答案 10 :(得分:9)

Imran的回答重新针对Java 7:

    WebDriverWait wait = new WebDriverWait(driver, 30);

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

答案 11 :(得分:5)

在此等待中明确等待或有条件等待,直到给出此条件。

WebDriverWait wait = new WebDriverWait(wb, 60);
wait.until(ExpectedConditions.elementToBeClickable(By.name("value")));

这将等待每个网页元素60秒。

隐式使用等待页面上每个元素的等待,直到给定时间。

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

这将等待每个网页元素60秒。

答案 12 :(得分:4)

隐式使用等待页面上每个元素的等待直到给定时间。

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

这等待页面上的每个元素持续30秒。

另一个等待是在此等待中显式等待或有条件等待,直到给定条件。

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

在id中,只要加载页面,就会在页面上显着地显示静态元素id。

答案 13 :(得分:4)

我很惊讶谓词并不是第一选择,因为您通常知道下一步要在您正在等待加载的页面上与哪些元素进行交互。我的方法一直是构建谓词/函数,如waitForElementByID(String id)waitForElemetVisibleByClass(String className)等,然后在我需要的地方使用和重用它们,无论是页面加载还是页面内容更改我都是等等。

例如,

在我的测试课程中:

driverWait.until(textIsPresent("expectedText");

在我的测试类父级中:

protected Predicate<WebDriver> textIsPresent(String text){
    final String t = text;
    return new Predicate<WebDriver>(){
        public boolean apply(WebDriver driver){
            return isTextPresent(t);
        }
    };
}

protected boolean isTextPresent(String text){
    return driver.getPageSource().contains(text);
}

虽然这看起来很多,但它会为您重复检查 并且可以与最终的一起设置检查频率的间隔 超时前等待时间。此外,您将重用此类方法。

在此示例中,父类定义并启动了WebDriver driverWebDriverWait driverWait

我希望这会有所帮助。

答案 14 :(得分:3)

/**
 * 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;
        }
    });
}

如果只更改文档的一部分,您可以从文档更改为元素。

这项技术的灵感来自于来自asbasic的答案。

答案 15 :(得分:3)

NodeJS解决方案:

Nodejs 中,您可以通过promises ...

获取

如果您编写此代码,则可以确保在到达当时页面已完全加载...

driver.get('www.sidanmor.com').then(()=> {
    // here the page is fully loaded!!!
    // do your stuff...
}).catch(console.log.bind(console));

如果您编写此代码,您将导航,而selenium将等待3秒......

driver.get('www.sidanmor.com');
driver.sleep(3000);
// you can't be sure that the page is fully loaded!!!
// do your stuff... hope it will be OK...

来自Selenium文档:

this.get(url)→Thenable

安排命令导航到给定的URL。

返回在文档完成加载时将解析的承诺。

Selenium Documentation (Nodejs)

答案 16 :(得分:3)

<强> SeleniumWaiter

import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumWaiter {

      private WebDriver driver;

      public SeleniumWaiter(WebDriver driver) {
           this.driver = driver;
      }

      public WebElement waitForMe(By locatorname, int timeout){
           WebDriverWait wait = new WebDriverWait(driver, timeout);
           return wait.until(SeleniumWaiter.presenceOfElementLocated(locatorname));
      }

      public static Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
            // TODO Auto-generated method stub
            return new Function<WebDriver, WebElement>() {
                 @Override
                 public WebElement apply(WebDriver driver) {
                      return driver.findElement(locator);
                 }
            };
      }
 }

对你使用

_waiter = new SeleniumWaiter(_driver);

try {
   _waiter.waitForMe(By.xpath("//..."), 10);
} 
catch (Exception e) {
   // Error
}

答案 17 :(得分:2)

我的简单方法:

long timeOut = 5000;
    long end = System.currentTimeMillis() + timeOut;

        while (System.currentTimeMillis() < end) {

            if (String.valueOf(
                    ((JavascriptExecutor) driver)
                            .executeScript("return document.readyState"))
                    .equals("complete")) {
                break;
            }
        }

答案 18 :(得分:2)

您可以使用以下现有方法设置pageeLoadTimeout的时间 在下面的示例中,如果页面加载时间超过20秒,那么它将抛出页面重新加载的例外

 WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS)

答案 19 :(得分:2)

您可以明确等待元素显示在网页上,然后才能采取任何操作(例如element.click())

driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
        @Override
        public WebElement apply(WebDriver d) {
        return d.findElement(By.id("myDynamicElement"));
}});

这是我用于类似场景的方法,它运行正常。

答案 20 :(得分:2)

调用以下功能

public static boolean isloadComplete(WebDriver driver)
{
    return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
            || ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}

答案 21 :(得分:2)

使用WebDriver的Java绑定时,等待页面加载的最佳方法是使用带有PageFactory的Page Object设计模式。这允许您使用AjaxElementLocatorFactory,它只是作为所有元素的全局等待。它对诸如下拉框或复杂的javascript转换等元素有限制,但它会大大减少所需的代码量并加快测试时间。在这篇博文中可以找到一个很好的例子。假设对Core Java有基本的了解。

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

答案 22 :(得分:1)

你可以使用等待。在selenium中基本上有两种等待

  • 隐瞒等待
  • 明确等待

- 隐含等待

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

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

- 明确等待

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

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

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

答案 23 :(得分:1)

如果有人使用硒化物:

public static final Long SHORT_WAIT = 5000L; // 5 seconds
$("some_css_selector").waitUntil(Condition.appear, SHORT_WAIT);

更多条件可以在这里找到: http://selenide.org/javadoc/3.0/com/codeborne/selenide/Condition.html

答案 24 :(得分:1)

就我而言,我使用以下内容来了解​​页面加载状态。在我们的应用程序中加载gif存在,我按如下方式听它们以消除脚本中不必要的等待时间。

public static void processing(){ 
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='Msgpanel']/div/div/img")));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@id='Msgpanel']/div/div/img")));
}

xpath在HTML DOM中定位gif的位置。 在此之后,您还可以实现您的操作方法Click。

public static void click(WebElement elementToBeClicked){
    WebDriverWait wait = new WebDriverWait(driver, 45);
    wait.until(ExpectedConditions.visibilityOf(element));
    wait.until(ExpectedConditions.elementToBeClickable(element)); 
    wait.ignoring(NoSuchElementException.class).ignoring(StaleElementReferenceException.class); elementToBeClicked.click(); 
 }

答案 25 :(得分:1)

所有这些答案都需要太多代码。这应该很简单,因为它很常见。

为什么不只是使用Webdriver注入一些简单的javascript并进行检查。 这是我在webscraper类中使用的方法。 即使您不懂js,JavaScript还是非常基本的。

    def js_get_page_state(self):        
    """
    Javascript for getting document.readyState
    :return: Pages state.

    More Info: https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
    """
    ready_state = self.driver.execute_script('return document.readyState')
    if ready_state == 'loading':
        self.logger.info("Loading Page...")
    elif ready_state == 'interactive':
        self.logger.info("Page is interactive")
    elif ready_state == 'complete':
        self.logger.info("The page is fully loaded!")
    return ready_state

答案 26 :(得分:0)

对于使用 java 8 以后的程序员可以使用以下代码使用显式等待来等待页面加载。

JavascriptExecutor js = (JavascriptExecutor) driver;  
new WebDriverWait(driver, 10).until(webDriver ->
(js).executeScript("return document.readyState;").equals("complete"));

注意:在我上面的代码中使用了 Lambda 表达式,它只在 Java 8 以后的版本中可用。

对于使用较低版本 Java 即 Java 8 以下的程序员可以使用:

ExpectedCondition<Boolean> cond = new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver input) {
        JavascriptExecutor js = (JavascriptExecutor) driver; 
        return js.executeScript("return document.readyState;").equals("complete");
            }
        };
         
    new WebDriverWait(driver, 100).until(cond);

答案 27 :(得分:0)

我使用node + selenium-webdriver(现在版本是3.5.0)。我为此做的是:

var webdriver = require('selenium-webdriver'),
    driver = new webdriver.Builder().forBrowser('chrome').build();
;
driver.wait(driver.executeScript("return document.readyState").then(state => {
  return state === 'complete';
}))

答案 28 :(得分:0)

public static int counter = 0;

public void stepGeneralWait() {

    boolean breakIt = true;

    while (true) {
        breakIt = true;
        try {

            do{
                // here put e.g. your spinner ID
                Controller.driver.findElement(By.xpath("//*[@id='static']/div[8]/img")).click();
                Thread.sleep(10000);

                counter++;

                if (counter > 3){
                    breakIt = false;

                }
            }
            while (breakIt);



        } catch (Exception e) {
            if (e.getMessage().contains("element is not attached")) {
                breakIt = false;
            }
        }
        if (breakIt) {
            break;
        }

    }

    try {
        Thread.sleep(12000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

答案 29 :(得分:0)

这是当前most upvoted answer的Java 8版本:

WebDriverWait wait = new WebDriverWait(myDriver, 15);
wait.until(webDriver -> ((JavascriptExecutor) myDriver).executeScript("return document.readyState").toString().equals("complete"));

myDriverWebDriver对象(先前已声明)。

注意:请注意,此方法(document.readyState)仅检查DOM。

答案 30 :(得分:0)

使用以下代码,页面加载非常简单。

public void PageLoad(IWebDriver driver, By by)
{
    try
    {
        Console.WriteLine("PageLoad" + by);
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        wait.Until(ExpectedConditions.ElementIsVisible(by));
        wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); // 30 seconds wait until element not found. 
        wait.Until(ExpectedConditions.ElementToBeClickable(by));


    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
        Assert.Fail("Element not found!")
    }
}

我希望这会对你有所帮助。

答案 31 :(得分:0)

我看到的最好的方法是利用stalenessOf预期的条件,等待旧页面变得陈旧。

示例:

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement oldHtml = driver.findElement(By.tagName("html"));
wait.until(ExpectedConditions.stalenessOf(oldHtml));

旧HTML标记变为陈旧,等待十秒钟,如果没有,则抛出异常。

答案 32 :(得分:0)

您可以使用此代码段来加载页面:

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

或者您可以使用服务员来加载任何元素并在该页面上变得可见/可点击,最有可能在加载结束时加载,如:

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

答案 33 :(得分:0)

private static void checkPageIsReady(WebDriver driver) {
    JavascriptExecutor js = (JavascriptExecutor) driver;

    // Initially bellow given if condition will check ready state of page.
    if (js.executeScript("return document.readyState").toString().equals("complete")) {
        System.out.println("Page Is loaded.");
        return;
    }

    // This loop will rotate for 25 times to check If page Is ready after
    // every 1 second.
    // You can replace your value with 25 If you wants to Increase or
    // decrease wait time.
    for (int i = 0; i < 25; i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        // To check page ready state.
        if (js.executeScript("return document.readyState").toString().equals("complete")) {
            break;
        }
    }
}

答案 34 :(得分:0)

您可以尝试使用此代码让页面完全加载,直到找到元素。

public void waitForBrowserToLoadCompletely() {
    String state = null;
    String oldstate = null;
    try {
        System.out.print("Waiting for browser loading to complete");

        int i = 0;
        while (i < 5) {
            Thread.sleep(1000);
            state = ((JavascriptExecutor) driver).executeScript("return document.readyState;").toString();
            System.out.print("." + Character.toUpperCase(state.charAt(0)) + ".");
            if (state.equals("interactive") || state.equals("loading"))
                break;
            /*
             * If browser in 'complete' state since last X seconds. Return.
             */

            if (i == 1 && state.equals("complete")) {
                System.out.println();
                return;
            }
            i++;
        }
        i = 0;
        oldstate = null;
        Thread.sleep(2000);

        /*
         * Now wait for state to become complete
         */
        while (true) {
            state = ((JavascriptExecutor) driver).executeScript("return document.readyState;").toString();
            System.out.print("." + state.charAt(0) + ".");
            if (state.equals("complete"))
                break;

            if (state.equals(oldstate))
                i++;
            else
                i = 0;
            /*
             * If browser state is same (loading/interactive) since last 60
             * secs. Refresh the page.
             */
            if (i == 15 && state.equals("loading")) {
                System.out.println("\nBrowser in " + state + " state since last 60 secs. So refreshing browser.");
                driver.navigate().refresh();
                System.out.print("Waiting for browser loading to complete");
                i = 0;
            } else if (i == 6 && state.equals("interactive")) {
                System.out.println(
                        "\nBrowser in " + state + " state since last 30 secs. So starting with execution.");
                return;
            }

            Thread.sleep(4000);
            oldstate = state;

        }
        System.out.println();

    } catch (InterruptedException ie) {
        ie.printStackTrace();
    }
}

答案 35 :(得分:0)

使用if条件和任何存在的元素

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

答案 36 :(得分:0)

  1. WebDriver driver = new ff / chrome / anyDriverYouWish(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 等待最多10秒。

  2. WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOf(WebElement element));

  3. FluentWait<Driver> fluentWait; fluentWait = new FluentWait<>(driver).withTimeout(30, TimeUnit.SECONDS) .pollingEvery(200, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class);

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

答案 37 :(得分:0)

driver.asserts().assertElementFound("Page was not loaded",
By.xpath("//div[@id='actionsContainer']"),Constants.LOOKUP_TIMEOUT);

答案 38 :(得分:0)

最简单的方法就是等待一些元素出现在加载的页面上。

如果您想在页面加载后点击某个按钮,可以使用等待并单击:

await().until().at.most(20, TimeUnit.Seconds).some_element.isDisplayed(); // or another condition
getDriver().find(some_element).click;

答案 39 :(得分:0)

How to get Selenium to wait for page load after a click 提供了以下有趣的方法:

  1. 从旧页面存储对WebElement的引用。
  2. 点击链接。
  3. 继续调用WebElement上的操作,直到StaleElementReferenceException被抛出。
  4. 示例代码:

    WebElement link = ...;
    link.click();
    new WebDriverWait(webDriver, timeout).until((org.openqa.selenium.WebDriver input) ->
    {
        try
        {
            link.isDisplayed();
            return false;
        }
        catch (StaleElementReferenceException unused)
        {
            return true;
        }
    });
    

答案 40 :(得分:-1)

There are 2 types of waits available in Webdriver/Selenium 2 software testing tool. One of them is Implicit wait and another one is explicit wait. Both (Implicit wait and explicit wait) are useful for waiting in WebDriver. Using waits, we are telling WebDriver to wait for a certain amount of time before going to next step.You can use implicit wait for page load waiting.

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

答案 41 :(得分:-1)

对于隐式等待,您可以使用以下内容:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

为了使网页等待特定对象可见或某些条件为真。您可以使用Web驱动程序的等待羽毛。

//120 is maximum number of seconds to wait.
WebDriverWait wait = new WebDriverWait(driver,120);  
wait.until(ExpectedConditions.elementToBeClickable("CONDITITON"));

Java 中,另一个选择是让线程在特定时间内休眠。

Thread.sleep(numberOfSeconds*1000); 
//This line will cause thread to sleep for seconds as variable

我创建了一个简化thread.sleep方法的方法

public static void wait_time(int seconds){
    try {
        Thread.sleep(seconds*1000);
        }catch (InterruptedException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

将该方法用作wait_time(10);线程将休眠10秒钟。

答案 42 :(得分:-1)

在python中,你可以简单地使用:

driver.implicitly_wait(30)

答案 43 :(得分:-2)

此代码将等待,直到页面上的所有元素都加载到DOM中。

WebDriver driver = new WebDriver();
WebDriverWait wait = new WebDriverWait(driver, timeout);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*")));

答案 44 :(得分:-2)

使用此功能

public void waitForPageLoad(ChromeDriver d){
        String s="";
        while(!s.equals("complete")){
        s=(String) d.executeScript("return document.readyState");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

    }

答案 45 :(得分:-2)

我不认为隐含的等待是你想要的。试试这个:

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

documentation

中的更多信息

答案 46 :(得分:-2)

使用:

driver.manage().timeOuts().implicitlyWait(10, TimeUnit.SECONDS);

这意味着在网页上搜索元素可能需要一些时间来加载。在抛出异常之前设置implicitlyWaitTimeUnit显示您想要等待的方式(秒,分钟,小时和天)。

答案 47 :(得分:-4)

隐含和明确的等待更好。

但是如果您在Java中处理异常,那么您可以使用它来等待页面重新加载:

Thead.sleep(1000);
相关问题