expected_conditions(EC)与元素相关的常用方法
与元素Element相关的 expected_conditions分为存在、可见、可点击、不可见/消失、属性/文本、选中状态等几类引用from selenium.webdriver.support import expected_conditions as EC1. 元素存在Presence等待元素出现在 DOM 中不一定可见elem wait.until(EC.presence_of_element_located((By.ID, username)))等待所有匹配的元素都出现在 DOM 中返回列表。elems wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, item)))2. 元素可见Visibility等待元素存在且可见宽高0不被隐藏btn wait.until(EC.visibility_of_element_located((By.ID, submit)))等待所有匹配的元素都可见返回列表。items wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, .list li)))3. 元素可点击Clickable等待元素可见且启用非 disabled常用于按钮。button wait.until(EC.element_to_be_clickable((By.XPATH, //button[text()提交]))) button.click()4. 元素不可见或消失等待元素不可见或不存在于 DOM。常用于等待 loading 遮罩消失。wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, loading-spinner)))等待一个已存在的元素从 DOM 中移除页面刷新或替换。old_elem driver.find_element(By.ID, old) # 触发页面刷新... wait.until(EC.staleness_of(old_elem))5. 元素文本与属性等待元素的文本内容包含指定文字。wait.until(EC.text_to_be_present_in_element((By.ID, status), 成功))等待元素的value属性包含指定文字用于输入框wait.until(EC.text_to_be_present_in_element_value((By.NAME, search), 关键词))等待元素包含某个属性不管值element_attribute_to_include(locator, attribute)等待元素的指定属性值包含给定子串。wait.until(EC.element_attribute_to_include((By.ID, link), href, baidu))6. 复选框/下拉框选中状态等待给定元素被选中element_to_be_selected(element)等待定位到的元素被选中element_located_to_be_selected(locator)等待元素的选中状态等于预期布尔值element_selection_state_to_be(element, is_selected)等待定位到的元素选中状态等于预期element_located_selection_state_to_be(locator, is_selected)checkbox driver.find_element(By.ID, agree) wait.until(EC.element_to_be_selected(checkbox))