Selenium Java中的isDisplayed()方法
创始人
2024-11-17 17:06:47
0

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

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

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

  • 我们将使用findElement()方法来查找元素。
  • 找到元素后,我们可以使用isDisplayed()方法。
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()方法。

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,如下面的例子所示。

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

相关内容

热门资讯

相较于以往!aapoker脚本... 相较于以往!aapoker脚本,aapoker透视脚本(透视)手段脚本(一贯有挂)-哔哩哔哩一、aa...
来临!hhpoker到底可以透... 来临!hhpoker到底可以透视码,约局吧开挂神器是真的吗(透视)学习教程(一贯有挂)1、让任何用户...
据统计!微友助手破解版,微信边... 据统计!微友助手破解版,微信边锋辅助下载(透视)神器-竟然有挂1、玩家可以在微信边锋辅助下载透视最简...
透视攻略!心悦踢坑神器软件下载... 透视攻略!心悦踢坑神器软件下载,卡农大厅辅助(开挂)果然真的是有挂(有挂解惑)-哔哩哔哩1、让任何用...
于此同时!hhpoker怎么破... 于此同时!hhpoker怎么破解,hhpoker辅助挂(透视)妙招脚本(原来真的是有挂)-哔哩哔哩1...
近年来!hhpoker软件靠谱... 近年来!hhpoker软件靠谱吗,wepoker有没有挂(透视)手册教程(都是真的有挂)1、实时hh...
今年以来!雀神广东麻雀智能科技... 今年以来!雀神广东麻雀智能科技贴吧,闲逸软件可以控制的吗(透视)器-果然真的有挂暗藏猫腻,小编详细说...
透视ai代打!途游游戏辅助器免... 透视ai代打!途游游戏辅助器免费,天酷游戏交易平台(开挂)确实存在有挂(有挂秘诀)-哔哩哔哩在进入途...
有消息称!aapoker安装包... 有消息称!aapoker安装包怎么使用,aapoker辅助怎么用(透视)攻略脚本(一贯有挂)-哔哩哔...
于此同时!pokemmo手机版... 于此同时!pokemmo手机版透视脚本,wepoker透视苹果系统(透视)练习教程(其实真的有挂);...