The difference between explicit wait and implicit wait in selenium

What are explicit waits and implicit waits? 
Explicit wait is conditional wait
Implicit wait is unconditional wait

Implicit wait
When the implicit wait is used to execute the test, if WebDriver does not find the element in the DOM, it will continue to wait, and after the set time is exceeded, an exception that the element cannot be found will be thrown. In other words, when the lookup element or the element does not appear immediately, the implicit wait will wait a period of time before looking up the DOM, the default time is 0,
from selenium import webdriver browser = webdriver.Chrome() browser.implicitly_wait(10)#Waiting for ten seconds to load, it will throw an exception, if it is loaded within 10 seconds, it will return normally browser.get('https://www.zhihu.com/explore') input = browser.find_element_by_class_name('zu-top-add-question') print(input) explicit wait Specify a waiting condition and a maximum waiting time, the program will judge whether the condition is satisfied within the waiting time, if it is satisfied, it will return, if it is not satisfied, it will continue to wait, and an exception will be thrown if the time is exceeded
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)

title_is title is something
title_contains title contains something
presence_of_element_located element is loaded out, passing in a positioning tuple, such as (By.ID, 'p')
visibility_of_element_located element is visible, pass in a positioning tuple
visibility_of Visible, pass in the element object
presence_of_all_elements_located all elements loaded out
text_to_be_present_in_element an element text contains a text
text_to_be_present_in_element_value An element value contains a text
frame_to_be_available_and_switch_to_it frame load and switch
invisibility_of_element_located element is not visible
element_to_be_clickable element is clickable
staleness_of determines whether an element is still in the DOM, and can determine whether the page has been refreshed
element_to_be_selected element can be selected, pass element object
element_located_to_be_selected element can be selected, pass in a positioning tuple
element_selection_state_to_be passes in the element object and state, returns True if equal, otherwise returns False
element_located_selection_state_to_be passes in the positioning tuple and state, returns True if equal, otherwise returns False
alert_is_present whether Alert appears

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161090&siteId=291194637