在硒中使用隐式等待 [英] Using implicit wait in selenium
问题描述
我是初学者.我了解基本等待的内容,但是我对互联网上的不同教程如何放置并解释它感到困惑.例如,在下面的代码中,将其放置在加载URL之前.那么,是否只是等待URL加载或查找元素或两者兼而有之?的确,如果我在try块中使用一次隐式等待,它是否适用于我在代码中执行的每个元素搜索?
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
ImplicitWait
ImplicitWait ="http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long-java.util.concurrent.TimeUnit-" rel ="nofollow noreferrer > Java Docs 用于指定 WebDriver 实例(即 driver )在搜索元素时应等待的时间(如果元素不立即出现在列表中). NANOSECONDS ,的 HTML DOM 术语MICROSECONDS , MILLISECONDS , SECONDS ,分钟,小时或天尝试查找一个或多个元素(如果不是立即可用)时.默认设置为 0 ,这意味着driver
当找到指令以查找一个或多个元素时,搜索开始并且结果立即可用.
在这种情况下,在重新加载网页后,可能立即搜索到一个或多个元素.因此,您的自动化框架可能会遇到以下任何一种情况例外:
因此,我们介绍 ImplicitWait .通过引入 ImplicitWait ,驱动程序将轮询 DOM树直到在配置的时间内找到该元素,然后在抛出 WebDriver 实例,即 driver 能够将这种配置保留到其生命周期.但是,如果您需要将 WebDriver 实例的粗略时间(即 driver 更改为 wait ),则可以按以下方式重新配置它:>
-
Python :
driver.implicitly_wait(5)
-
Java :
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
-
DotNet :
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
如果您想随时取消 ImplicitWait
,可以按以下方式重新配置它:
-
Python :
driver.implicitly_wait(0)
-
Java :
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
-
DotNet :
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
回答您的问题
- ...等待URL ... :否, ImplicitWait 对页面加载没有影响.
- ...用于查找元素... :是的, ImplicitWait 将定义
WebDriver
实例将等待寻找元素的时间的粗略值,或者元素. - ...隐式等待一次... :是的,您只需配置一次 ImplicitWait ,它在 WebDriver 实例.
- ...每个元素搜索... :是的,在调用
findElement()
或findElements()
时适用.
I am a beginner. I understand what waits basically does but I am confused over how different tutorials over the internet place it and explain it. For example, in the below code it is placed before loading the URL. So, is it only to wait for the URL to be loaded or for finding the element or both? Is is true that if I use an implicit wait once in my try block, it will be applicable for every element search I am performing in my code?
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
ImplicitWait
ImplicitWait as per the Java Docs is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS or DAYS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver
when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.
In this case, after a fresh loading of a Webpage an element or elements may be / may not be found on an immediate search. So your Automation Framework may be facing any of these exceptions:
Hence we introduce ImplicitWait. By inducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.
Python:
driver.implicitly_wait(10)
Java:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Finally, once you set the ImplicitWait, the WebDriver instance i.e. the driver is able to carry this configuration till its lifetime. But if you need to change the coarse of time for the WebDriver instance i.e. the driver to wait then you can reconfigure it as follows:
Python:
driver.implicitly_wait(5)
Java:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
If at any point of time you want to nullify the ImplicitWait
you can reconfigure it as follows:
Python:
driver.implicitly_wait(0)
Java:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
Answering your questions
- ...Wait for the URL... : No, ImplicitWait have no effect on page loading.
- ...For finding the element... : Yes, ImplicitWait will define the coarse of time the
WebDriver
instance will wait looking out for the element or elements. - ...Implicit wait once... : Yes, you need to configure ImplicitWait only once and it is applicable throughout the lifetime of the WebDriver instance.
- ...Every element search... : Yes, applicable when ever
findElement()
orfindElements()
is invoked.
这篇关于在硒中使用隐式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!