Selenium WebDriver 的 isDisplayed() 方法如何工作?
我们可以使用 Selenium webdriver 中的**isDisplayed()**方法。此方法检查 web 元素是否在页面上可见。如果可见,则该方法返回真值,否则返回假值。
首先,我们必须使用 id、类、名称、xpath 或 css 等定位符之一标识元素,然后在其上应用 isDisplayed() 方法。
语法
boolean s= driver.findElement(By.id("txt-bx")).isDisplayed();让我们检查元素**关于 Tutorials Point 的职业生涯**是否显示在页面上。因为它可用,所以它将返回一个真值。

示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class VisibleElement{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/about/about_careers.htm");
// identify element
WebElement p=driver.findElement(By.xpath("//h1"));
//isDisplayed() to check if element visible
boolean s= p.isDisplayed();
System.out.println("The return value: " + s);
driver.close();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP