首页 > 解决方案 > 如何检查列表中的所有网络元素是否显示

问题描述

带有文本链接的页脚

我想验证“更多信息”页脚部分下的“关于我们”、“联系我们”和“常见问题”文本链接是否显示

我怎样才能检查它?我是否必须获取每个列表 web 元素的 xpath,然后检查它是否显示在网页上?或一次所有网络元素的列表,然后跟踪for循环?

还帮助我为“关于我们”元素编写 xpath

标签: listseleniumfor-loopxpathheader

解决方案


您可以一次验证全部,也可以单独验证,尝试下面的代码,它将获取所有选项,验证这些选项是否存在并检查它们是否显示:

// Get all the options using the below line
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='footer-section']//a"));

// Check they are present or not?
if(elements.size() > 0) {
    System.out.println("=> The Options are present...");
} else {
    System.out.println("=> The Options are nor present...");
}

// Check they are displayed or not?
for(WebElement element : elements) {
    System.out.println(element.getText()+" is displayed? "+element.isDisplayed());
}

如果要单独验证,则需要执行以下操作:

driver.findElement(By.xpath("//div[@class='footer-section']//a[text()='About Us']")).isDisplayed();

推荐阅读