原文:https://www.cnblogs.com/lgh344902118/p/6015593.html
webdriver三种等待方法
1.使用WebDriverWait
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()
presence_of_element_located可以替换为
- title_is
- title_contains
- presence_of_element_located
- visibility_of_element_located
- visibility_of
- presence_of_all_elements_located
- text_to_be_present_in_element
- text_to_be_present_in_element_value
- frame_to_be_available_and_switch_to_it
- invisibility_of_element_located
- element_to_be_clickable - it is Displayed and Enabled.
- staleness_of
- element_to_be_selected
- element_located_to_be_selected
- element_selection_state_to_be
- element_located_selection_state_to_be
- alert_is_present
By.ID中的ID可替换为‘CLASS_NAME‘, ‘CSS_SELECTOR‘, ‘ID‘, ‘LINK_TEXT‘, ‘NAME‘, ‘PARTIAL_LINK_TEXT‘, ‘TAG_NAME‘, ‘XPATH‘
2、隐式等待,相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。
默认设置是0。
一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。
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")
3、强制等待
import time time.sleep(10)
原文地址:https://www.cnblogs.com/eedc/p/8642786.html
时间: 2024-11-07 08:20:30