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
相关推荐
知凡D10 小时前
python脚本打包成exe后,对其引用的日历库实时更新-动态化加载模块
python·测试工具
Wpa.wk10 小时前
接口测试-Postman接口测试流程小练习2
测试工具·postman
阿蔹10 小时前
UI测试自动化-Web-Python-Selenium-2-元素操作、浏览器操作
前端·python·selenium·ui·自动化
程序员杰哥13 小时前
Postman设置接口关联,实现参数化
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
oh-pinpin13 小时前
【jmeter】-脚本-导入添加接口(F12复制 cURL )
测试工具·jmeter·压力测试
软件测试小仙女15 小时前
告别手工分析!Python+HAR一键生成页面性能测试报告
自动化测试·软件测试·python·测试开发·测试工具·性能测试·测试报告
汽车仪器仪表相关领域15 小时前
亲历机动车排放检测升级:南华NHA-604/605测试仪的实战应用与经验沉淀
人工智能·功能测试·测试工具·安全·汽车·压力测试
BullSmall15 小时前
Anroid、IOS、鸿蒙、浏览器的兼容性测试方案
测试工具·可用性测试
fzm529816 小时前
嵌入式软件单元测试中AI自动化与人工检查的协同机制研究:基于专业工具的实证分析
c语言·测试工具·单元测试·自动化