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 Script
may be facing any of these exceptions:
Hence we introduce ImplicitWait. By introducing 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);
Answer to 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 theWebDriver
instance will wait looking out for the element or elements.Implicit wait once
: Yes, you need to configureImplicitWait
only once and it is applicable throughout the lifetime of theWebDriver
instance.Every element search
: Yes, applicable when everfindElement()
orfindElements()
is invoked.
yes, implicit_wait
is globally applicable. so once you set it's applied to all the element.
I would not suggest to use implicit_wait
unless your application is too slow. You could use explicit wait
or any other wait based on your requirement from the following page.
it's a JAVADOC but implementation should be same for python as well.
Implicit wait is applicable for all the web elements where as Explicit wait is applicable only for the element it is specified.
Explicit wait is more intelligent and are really use full in handling Ajax on the other hand implicit wait is generally used to handle application sync issues.
来源:https://stackoverflow.com/questions/45672693/using-implicit-wait-in-selenium