如果 findElement 操作完成,隐式等待是否会继续?
Will the Implicit wait will move on if findElement action is complete?
隐式等待 :如果设置了等待,它将为每个 findElement
/findElements
调用等待指定的时间。如果操作未完成,它将抛出异常。
假设我们将隐式等待设置为 10 秒。我的问题是,如果 findElement
操作在 10 秒之前完成,selenium 会继续下一步吗?
答案:
Yes. It will continue with the next step if it finds the element before the implicit timeout is hit.
概念验证:
@Test
public void test29800926() {
driver.get("http://ddavison.io/tests/getting-started-with-selenium.htm");
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
System.out.println(driver.findElement(By.id("click")).getText());
}
它没有等待我将隐式等待设置为 (30000ms / 1000 = 30sec) 的总共 30 秒,而是立即找到它并继续打印元素的文本。
是。如果要查找的元素是 [=18=,则设置隐式等待会导致驱动程序对象 等待 设置的时间]没有立即找到。驱动程序对象每 500 毫秒持续轮询 DOM,直到它找到元素或超时到期。
这是official Selenium documentation page的解释:
An implicit wait is to tell WebDriver to poll the DOM for a certain
amount of time when trying to find an element or elements if they are
not immediately available. The default setting is 0. Once set, the
implicit wait is set for the life of the WebDriver object instance.
所以,为了简短地回答您的问题,是的,它会在找到要查找的元素后立即继续执行后续步骤。您可能还从一个简单的实验中了解到,就像@sircapsalot 在他的回答中显示的那样。
隐式等待 :如果设置了等待,它将为每个 findElement
/findElements
调用等待指定的时间。如果操作未完成,它将抛出异常。
假设我们将隐式等待设置为 10 秒。我的问题是,如果 findElement
操作在 10 秒之前完成,selenium 会继续下一步吗?
答案:
Yes. It will continue with the next step if it finds the element before the implicit timeout is hit.
概念验证:
@Test
public void test29800926() {
driver.get("http://ddavison.io/tests/getting-started-with-selenium.htm");
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
System.out.println(driver.findElement(By.id("click")).getText());
}
它没有等待我将隐式等待设置为 (30000ms / 1000 = 30sec) 的总共 30 秒,而是立即找到它并继续打印元素的文本。
是。如果要查找的元素是 [=18=,则设置隐式等待会导致驱动程序对象 等待 设置的时间]没有立即找到。驱动程序对象每 500 毫秒持续轮询 DOM,直到它找到元素或超时到期。
这是official Selenium documentation page的解释:
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
所以,为了简短地回答您的问题,是的,它会在找到要查找的元素后立即继续执行后续步骤。您可能还从一个简单的实验中了解到,就像@sircapsalot 在他的回答中显示的那样。