When we are doing WEB automation, we generally have to wait for the page elements to load before we can perform the operation, otherwise we will report the error that the elements cannot be found, which requires us to add the waiting time in some scenarios.
We usually use three waiting methods:
- Forced waiting
- Implicit waiting
- Display wait
1, Forced waiting
The sleep method of time module is used to realize the simplest and rough waiting method
code:
# coding = utf-8 from time import sleep from selenium import webdriver # Drive file path driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe' # Launch browser driver = webdriver.Chrome(executable_path=driverfile_path) # Open Baidu home page driver.get(r'https://www.baidu.com/') # Wait 3 seconds sleep(3) driver.find_element_by_css_selector("#kw").send_keys("selenium") # sign out driver.quit()
This is called forced waiting. No matter whether your browser is loaded or not, you have to wait for 3 seconds. Once 3 seconds arrive, continue to execute the following code. This waiting method is not recommended, which will seriously affect the execution speed of the code
2, Implicit waiting
Set a waiting time. If the web page is loaded within this waiting time, execute the next step; Otherwise, wait until the time expires before proceeding to the next step. This will also have a disadvantage. The program will wait for the whole page to load until it times out, but sometimes the element I need has been loaded long ago, but some other elements on the page load very slowly. I still have to wait for all the pages to load before I can take the next step.
code:
# coding = utf-8 from selenium import webdriver # Drive file path driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe' # Launch browser driver = webdriver.Chrome(executable_path=driverfile_path) # Open Baidu home page driver.get(r'https://www.baidu.com/') driver.find_element_by_css_selector("#kw").send_keys("selenium") driver.find_element_by_css_selector("#su").click() # Implicit wait 30 seconds driver.implicitly_wait(30) result = driver.find_elements_by_css_selector("h3.t>a") for i in result: print(i.text) # sign out driver.quit()
3, Display wait
Above, we mentioned a disadvantage of implicit waiting. What should I do if I want to execute the next step as soon as the elements I want are loaded? Here we need to use the display wait
Show WebDriverWait to be used
Cooperate with the until() and until() of this class_ With the not () method, you can wait flexibly according to the judgment conditions. Its main meaning is: the program checks every xx. If the condition is true, execute the next step. Otherwise, continue to wait until the set maximum time is exceeded, and then throw TimeoutException
Let's first take a look at the help document of WebDriverWait:
>>> help(WebDriverWait) Help on class WebDriverWait in module selenium.webdriver.support.wait: class WebDriverWait(builtins.object) | Methods defined here: | | __init__(self, driver, timeout, poll_frequency=0.5, ignored_exceptions=None) | Constructor, takes a WebDriver instance and timeout in seconds. | | :Args: | - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote) | - timeout - Number of seconds before timing out | - poll_frequency - sleep interval between calls | By default, it is 0.5 second. | - ignored_exceptions - iterable structure of exception classes ignored during calls. | By default, it contains NoSuchElementException only. | | Example: | from selenium.webdriver.support.ui import WebDriverWait | | element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_i d("someId")) | | is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleExcepti on)).\ | | until_not(lambda x: x.find_element_by_id("someId").is_displ ayed())
There are four main parameters:
Driver: Browser driver
timeout: wait time
poll_frequency: detection interval, 0.5s by default
ignored_exceptions: exception information after timeout. NoSuchElementException is thrown by default
code:
# coding = utf-8 from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait # Drive file path driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe' # Launch browser driver = webdriver.Chrome(executable_path=driverfile_path) # Open Baidu home page driver.get(r'https://www.baidu.com/') driver.find_element_by_css_selector("#kw").send_keys("selenium") driver.find_element_by_css_selector("#su").click() # The timeout is 30 seconds. Check every 0.2 seconds until the element of class="tt" appears text = WebDriverWait(driver,30,0.2).until(lambda x:x.find_element_by_css_selector(".tt")).text print(text) # sign out driver.quit()