我有一个场景,有一个表,当我搜索该表时,我将得到一个结果集,我需要验证搜索数据是否在搜索结果中可用。我大约有1000个li标签,并且只有一个通过其属性“样式”验证结果集的方法。对于结果集,li标记中没有'style'属性。
public boolean isAttribtuePresent(WebElement element, String attribute) {
Boolean ATTRIB_PRESENT = true;
try {
String value = element.getAttribute(attribute);
if (value.isEmpty() || value == "" || value == null) {
ATTRIB_PRESENT = true;
}
} catch (Exception e) {
ATTRIB_PRESENT = false;
}
return ATTRIB_PRESENT;
}
我尝试过这个,但是它正在验证所有LI标签
答案 0 :(得分:3)
更新:在对其进行测试之后,这应该可以解决问题:
private boolean isStyleAttributePresent(WebElement element) {
String attributeValue = element.getAttribute("style");
return attributeValue != null && !attributeValue.isEmpty();
}
旧答案:
正在寻找at the docs of the getAttribute() method:
返回: 属性/属性的当前值;如果未设置,则为null。
...,并假设您正在寻找没有设置<li>
属性的style
,这应该可以解决问题:
public boolean isStyleAttributePresent(WebElement element) {
return element.getAttribute("style") != null;
}
用于验证解决方案的演示:
/* ... */
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/home/kasptom/selenium/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("file:///home/kasptom/Dev/stack-java-maven/src/main/resources/my_page.html");
List<WebElement> lis = driver.findElements(By.tagName("li"));
List<WebElement> lisWithNoStyle = lis.stream()
.filter(SeleniumLiDemo::isStyleAttributePresent)
.collect(Collectors.toList());
System.out.format("Number of <li> detected %d, with no style %d%n", lis.size(), lisWithNoStyle.size());
}
private static boolean isStyleAttributePresent(WebElement element) {
String attributeValue = element.getAttribute("style");
return attributeValue != null && attributeValue.isEmpty();
}
/* ... */
my_page.html
<html>
<body>
<ul>
<li class="ui-selectlistbox-item ui-corner-all">ADL (Std. ProcID: 1)</li>
<li style="display: none" class="ui-selectlistbox-item ui-corner-all">ADL (Std. ProcID: 1)</li>
</ul>
</body>
</html>