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

相关内容

热门资讯

真的有挂!德普之星的辅助工具介... 真的有挂!德普之星的辅助工具介绍,九九联盟辅助神器,扑克教程(原来真的有挂);相信小伙伴都知道这个九...
真的有挂!wepoker怎么下... 《真的有挂!wepoker怎么下载游戏,欢乐互娱辅助器,技巧教程(一直真的有挂)》 欢乐互娱辅助器软...
真的有挂!德州hhpoker脚... 真的有挂!德州hhpoker脚本,衡阳丫丫字牌辅助,爆料教程(一直真的有挂);超受欢迎的衡阳丫丫字牌...
真的有挂!智星德州插件怎么下载... 真的有挂!智星德州插件怎么下载,闲聚鱼虾蟹软件脚本,高科技教程(果然真的有挂);闲聚鱼虾蟹软件脚本A...
真的是有挂!hhpoker德州... 真的是有挂!hhpoker德州辅助,小程序跑得快的辅助,黑科技教程(果然真的是有挂);小薇(透视辅助...
是有挂的!wepoker私人局... 是有挂的!wepoker私人局有透视吗,欢乐情怀怎么开挂,解密教程(一直是有挂的);欢乐情怀怎么开挂...
真的有挂!aapoker发牌逻... 真的有挂!aapoker发牌逻辑,wepoker内置辅助器,软件教程(一直真的有挂);wepoker...
真的有挂!cloudpoker... 真的有挂!cloudpoker辅助,来趣广西友玩脚本辅助,力荐教程(一贯真的有挂);来趣广西友玩脚本...
是有挂!wepokerplus... 是有挂!wepokerplus透视脚本免费,天天爱柳州辅助,必备教程(果然是有挂);天天爱柳州辅助软...
确实有挂!wepoker有人用... 确实有挂!wepoker有人用过吗,老友辅助器,曝光教程(其实确实有挂);一、老友辅助器AI软件牌型...