2015-07-21 97 views
1

说我有这样的代码:Selemium webdriver:驱动程序尝试使用隐式等待超时来查找元素的次数?

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

我有麻烦了解硒DOC这一行:隐式的等待,就是告诉webdriver的轮询DOM一定量的时间,试图找到一个元素时或元素,如果他们不立即可用。

那么这是否意味着驱动程序会在第一次尝试查找元素之前等待10秒?或者这是否意味着驱动程序会首先找到该元素,如果没有发现,请等待10秒,然后再次查找,如果找不到,则抛出超时错误?驱动程序试图总共找到两次元素?

+0

看看[QA](http://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-pageloading)。 [Ardesco](http://sqa.stackexchange.com/users/540/ardesco)的回答;隐式超时指定驱动程序在搜索元素时如果不立即出现,应该等待的时间量。 – lloyd

回答

1

您可以通过将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) 

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


什么内部发生是described in the docs here:对于10为秒,轮询DOM试图找到所需的元素忽略NoSuchElementException。当时间到了,并没有元素发现但它会抛出NoSuchElementException

隐含的等待是告诉webdriver的轮询DOM一定 的时间努力,如果他们 找到一种或多种元素时没有立即可用。默认设置为0.一旦设置, 隐式等待设置为WebDriver对象实例的生命周期。

+0

所以隐含的等待只是一个更好的方式来编写Thread.sleep()吗? – nieschumi

+0

感谢您的详细解答,监控Json协议日志看起来真的有助于了解幕后的情况! – nieschumi

+0

@nieschumi高兴地帮忙!谢谢! – alecxe

0

如果元素driver.findElement(By.id(“nothing”));可在您的网页上找到webelement并继续到下一个代码。如果该webelement不存在于网页中,则驱动程序将等待10秒钟,因为您隐式等待10秒钟。最后测试将失败,因为未找到元素。所以只有一次驱动程序试图找到webelement。

相关问题