Selenium Java中的isDisplayed()方法

isDisplayed()方法用于确定元素是否可见。本文将详细讨论

的WebElement接口isDisplayed()方法。

  • 方法声明- boolean isDisplayed()
  • 它能做什么?此方法用于判断元素是否显示。这个方法节省了我们解析style属性以推断元素是否隐藏的时间。
  • 它会返回什么?如果元素在网页上可见,isDisplayed()方法将返回true。否则,它将返回false。
代码示例
让我们先以可见文本为例。

下面突出显示的元素是一个可见元素。这种元素存在于我们++Selenium游乐场网站++上。我们将在这个元素上使用isDisplayed()方法,看看会发生什么。

  • 我们将使用findElement()方法来查找元素。
  • 找到元素后,我们可以使用isDisplayed()方法。
java 复制代码
public class CodekruTest {
 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = driver.findElement(By.id("plainText"));
 
        System.out.println("Is element visible on webpage: " + element.isDisplayed());
    }
}

产出-

复制代码
Is element visible on webpage: true
现在,我们来看看一个隐藏的元素

我们可以在++同一个网页++上再次找到隐藏的元素(我们心爱的游乐场网站)。我们在下图中突出显示了隐藏的元素。

现在让我们在隐藏元素上使用isDisplayed()方法。

java 复制代码
public class CodekruTest {
 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = driver.findElement(By.id("hiddenText"));
 
        System.out.println("Is element visible on webpage: " + element.isDisplayed());
    }
}

产出-

复制代码
Is element visible on webpage: false
如果我们对null元素使用isDisplayed()方法会怎么样?

我们将得到一个NullPointerException,如下面的例子所示。

java 复制代码
public class CodekruTest {
 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = null;
 
        System.out.println("Is element visible on webpage: " + element.isDisplayed());
    }
}

产出-

复制代码
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.isDisplayed()" because "element" is null
相关推荐
安冬的码畜日常8 小时前
【JUnit实战3_13】第八章:mock 对象模拟技术在细粒度测试中的应用(上)
测试工具·junit·单元测试·junit5·mock模拟·mock对象·mock objects
天才测试猿13 小时前
Selenium定位元素的方法css和xpath的区别
css·自动化测试·软件测试·python·selenium·测试工具·测试用例
游戏开发爱好者813 小时前
Fiddler抓包实战教程 从安装配置到代理设置,详解Fiddler使用方法与调试技巧(HTTPHTTPS全面指南)
前端·测试工具·小程序·https·fiddler·uni-app·webview
程序员小远1 天前
selenium元素定位---(元素点击交互异常)解决方法
自动化测试·软件测试·python·selenium·测试工具·测试用例·交互
王者鳜錸1 天前
基于Selenium和AI的图像处理
图像处理·人工智能·selenium
2501_938774291 天前
Copilot 与测试工具协同?Mastering 课程中单元测试生成与结对编程的结合
测试工具·单元测试·copilot
安冬的码畜日常2 天前
【JUnit实战3_14】第八章:mock 对象模拟技术在细粒度测试中的应用(中):为便于模拟重构原逻辑的两种策略
测试工具·junit·重构·单元测试·多态·junit5·mock 模拟
.hopeful.2 天前
Selenium常用方法
selenium·测试工具