java - Selenium 网络驱动程序 : How many times does a driver try to find element with an implicit wait timeout?

标签 java selenium selenium-webdriver webdriver

假设我有这样的代码:

Webdriver driver = new ChromeDriver();
driver.manage().timeout().implicitWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("nothing"));

我很难理解 Selenium 文档中的这一行:隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不立即可用)时轮询 DOM 一段时间。

那么这是否意味着驱动程序将等待 10 秒才能首次尝试查找元素?或者这是否意味着驱动程序将首先找到该元素,如果没有找到,则等待10秒,然后再次查找,如果没有找到则抛出超时异常?驱动程序总共尝试两次查找该元素?

最佳答案

实际上,您可以通过记录 JSON Wire protocol commands 来清除问题。进入 Chrome 服务日志。假设我们有以下 Python 代码(作为示例):

from selenium import webdriver

driver = webdriver.Chrome(service_log_path="/tmp/log")
driver.get("http://www.google.com")

driver.find_element_by_css_selector("strange.non.existing.element")

driver.quit()

这里我们立即得到一个NoSuchElementException,并且在/tmp/log中我们有:

[2.134][INFO]: COMMAND Navigate {
   "sessionId": "920fbde18d13995672cbbdd0a15e905a",
   "url": "http://www.google.com"
}
[2.195][INFO]: Waiting for pending navigations...
[2.239][INFO]: Done waiting for pending navigations
[2.593][INFO]: Waiting for pending navigations...
[3.704][INFO]: Done waiting for pending navigations
[3.704][INFO]: RESPONSE Navigate
[3.706][INFO]: COMMAND FindElement {
   "sessionId": "920fbde18d13995672cbbdd0a15e905a",
   "using": "css selector",
   "value": "strange.non.existing.element"
}
[3.706][INFO]: Waiting for pending navigations...
[3.706][INFO]: Done waiting for pending navigations
[3.720][INFO]: Waiting for pending navigations...
[3.720][INFO]: Done waiting for pending navigations
[3.720][INFO]: RESPONSE FindElement no such element
  (Session info: chrome=43.0.2357.134)

现在,让我们将隐式等待设置为 10 秒:

from selenium import webdriver

driver = webdriver.Chrome(service_log_path="/tmp/log")
driver.get("http://www.google.com")

# setting the implicit wait
driver.implicitly_wait(10)

driver.find_element_by_css_selector("strange.non.existing.element")

driver.quit()

现在,如果我们查看日志:

[1.996][INFO]: COMMAND Navigate {
   "sessionId": "657700804d0d8f71b2bfee6dc222c289",
   "url": "http://www.google.com"
}
[2.073][INFO]: Waiting for pending navigations...
[2.106][INFO]: Done waiting for pending navigations
[2.477][INFO]: Waiting for pending navigations...
[3.371][INFO]: Done waiting for pending navigations
[3.371][INFO]: RESPONSE Navigate
[3.374][INFO]: COMMAND SetImplicitWait {
   "ms": 10000.0,
   "sessionId": "657700804d0d8f71b2bfee6dc222c289"
}
[3.374][INFO]: RESPONSE SetImplicitWait
[3.376][INFO]: COMMAND FindElement {
   "sessionId": "657700804d0d8f71b2bfee6dc222c289",
   "using": "css selector",
   "value": "strange.non.existing.element"
}
[3.376][INFO]: Waiting for pending navigations...
[3.376][INFO]: Done waiting for pending navigations
[13.410][INFO]: Waiting for pending navigations...
[13.410][INFO]: Done waiting for pending navigations
[13.410][INFO]: RESPONSE FindElement no such element
  (Session info: chrome=43.0.2357.134)

您可以看到,只有一个 FindElement 命令发送到 WebDriver,但响应返回,并且仅在 10 秒延迟后才抛出 NoSuchElementException

<小时/>

内部发生的是described in the docs here :在 10 秒内,它会轮询 DOM,尝试查找所需的元素,而忽略 NoSuchElementException。当时间到了但还没有找到元素时,它会抛出 NoSuchElementException:

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.

关于java - Selenium 网络驱动程序 : How many times does a driver try to find element with an implicit wait timeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550843/

相关文章:

selenium - 您可以在同一个测试/框架中将 TestNG 和 JUnit 断言混合在一起吗?

java - 使用 JAXB 将多个 XML 元素的内容提取为文本

java - 当显示关闭时如何保持 IntentService 运行?

java - 我们可以使用scala来调试Java程序吗

javascript - Selenium: "is not clickable at point"如何等待所有脚本加载完毕

java - 如何更有效地在 3 个不同的下拉菜单中输入日期、月份、年份

selenium - 如何针对 Safari 运行 Nightwatch 测试?

c# - Selenium 测试在 IE11 中不起作用

java - 在大数组中搜索最小数字

java - URL 无法使用 WebDriver 打开