如何识别没有样式属性的<li>标签?

赛义德·艾哈迈德(Syed Ahmed):

我有一个有一个表的场景,当我搜索该表时,我将得到一个结果集,我需要验证搜索结果中是否有搜索数据。我大约有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标签

kasptom:

更新:经过测试,这应该可以解决问题:

private boolean isStyleAttributePresent(WebElement element) {
    String attributeValue = element.getAttribute("style");
    return attributeValue != null && !attributeValue.isEmpty();
}

旧答案:

查看getAttribute()方法的文档:

返回:属性/属性的当前值;如果未设置该值,则返回null。

...并假设您正在寻找<li>没有set 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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章