1、显式等待
一个显式等待是你定义的一段代码,用于等待某个条件发生然后再继续执行后续代码。显式等待是等元素加载!!!
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
driver = webdriver.Firefox()
driver.get(“http://somedomain/url_that_delays_loading“)
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, “myDynamicElement”)))
finally:
driver.quit()
2、隐式等待
相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。隐式等待是等页面加载,而不是元素加载!!!(隐式等待就是针对页面的,显式等待是针对元素的。)
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get(http://www.xxx.com)
myDynamicElement = driver.find_element_by_id(“ElementID”)