在 Selenium WebDriver 中,
这个方法在自动化测试中非常有用,特别是在需要确保某个元素已经加载并且可以与之交互之前。
等待一个已经找到的元素变为可见。
等待某个元素不仅是可见的,而且是可以点击的。
等待某个元素从 DOM 中消失或者变得不可见。
通过这些信息,可以更好地理解和使用
visibilityOfElementLocated 方法
wait.until(ExpectedConditions.visibilityOfElementLocated(locator))
是一个重要的显式等待方法。它会等待指定的元素不仅出现在 DOM 中,而且是可见的(即元素的宽度和高度都大于 0)。
功能描述
expected_conditions
模块,用于检查元素是否在页面上可见。
使用示例
Python 示例
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'element_id')))
Java 示例
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")))
注意事项
expected_conditions
模块。By.ID
, By.XPATH
, By.CSS_SELECTOR
等)。
相关方法
visibility_of(element)
element_to_be_clickable(locator)
invisibility_of_element_located(locator)
总结
wait.until(ExpectedConditions.visibilityOfElementLocated(locator))
方法来编写更健壮的自动化测试脚本。
Selenium WebDriver visibilityOfElementLocated 方法
作者:zvvq博客网
免责声明:本文来源于网络,如有侵权请联系我们!