In UI automation testing, you will often encounter a bad network environment, resulting in elements that cannot be found. If an error is reported, the wait in selenium will be used at this time. There are three waiting methods in selenium
time (fixed waiting)
Using format: time.sleep(seconds)
Although you can customize the waiting in this way However, in the case of a good network environment, it will continue to wait according to the set time, which leads to prolonged automation time and is not recommended.
implicitly_wait (hermit wait)
Use format:driver.implicitly_wait(seconds)
The hermit wait is actually set up The longest waiting time. If the web page is loaded within the specified time, execute the next step, otherwise wait until the time is over, and then execute the next step.
Hermit waiting also has shortcomings. We all know that js is usually loaded at the end of the body. In fact, the page has been loaded at this time, but it is still waiting for all pages to load. The hermit wait is effective for the entire driver cycle.
You only need to set it once at the very beginning, and don’t use it as a fixed wait. At every step Add the hermit to wait.
WebDriverWait (display waiting)
1. First import the WebDriverWait package
from selenium .webdriver.support.wait import WebDriverWait
2 WebDriverWait parameters
Using format: WebDriverWait(driver,timeout,poll_frequency)
driver: Pass in the WebDriver instance, that is, we The driver in the above example
timeout: Timeout, the longest waiting time
< span style="font-size: 16px;">poll_frequency: The interval time between calling until or until_not methods, the default is 0.5 seconds
ignored_exceptions: Ignored exceptions. If an exception in this tuple is thrown during the call to until or until_not, the code will not be interrupted and continue to wait. If it is thrown For exceptions outside this tuple,
will interrupt the code and throw an exception. There is only NoSuchElementException by default.
There are two methods in this module: until and until not
until: Continue to execute when an element appears or what conditions are met
until not: Continue to execute when an element disappears or some conditions are not established
These two methods need to introduce a selenium module
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(driver,10,0.5)
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By