java - 如何将属性值与字符串进行比较

标签 java selenium

我正在使用下面的代码来获取网页上字段的属性:-

    List<WebElement> EngDesc = driver.findElements(By.xpath("//input[@id='EngDesc']"));
        System.out.println("List size: "+EngDesc.size());
        for(WebElement list : EngDesc ){
            System.out.println("search value: "+list.getAttribute("value"));
            if(list.getAttribute("Value").equals("CHARGEABLE")) {
                System.out.println("This is General");   
             }

下面是我得到的输出:-

List size: 1 search value: CHARGEABLE Exception in thread "main" java.lang.NullPointerException at newrequestFlow.Index.main(Index.java:81)

有人可以帮我解决这个问题吗?

最佳答案

您看到的错误说明如下:

NullPointerException at newrequestFlow.Index.main(Index.java:81)

findElements(By.xpath("//input[@id='EngDesc']")); 仅返回 1 条目。 最初在 for() 循环中,您使用 list.getAttribute("value")。但稍后您尝试使用 list.getAttribute("Value")

value and Value are different attribute and probably there were no attribute present as Value . Hence Null is returned and java.lang.NullPointerException is observed.

解决方案

List<WebElement> EngDesc = driver.findElements(By.xpath("//input[@id='EngDesc']"));
    System.out.println("List size: "+EngDesc.size());
    for(WebElement list : EngDesc )
    {
        System.out.println("search value: "+list.getAttribute("value"));
        if(list.getAttribute("value").equals("CHARGEABLE")) 
        {
            System.out.println("This is General");   
        }
    }

关于java - 如何将属性值与字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713279/

相关文章:

java - 使用文本打印二叉搜索树

java - 从 Web 服务下载大文件导致应用程序性能问题

java - 无法通过selenium单击工具提示中的链接

选择单选按钮的 Selenium 代码

javascript - 如何使用 selenium 在 Code Mirror 行中输入文本?

python - Linux 上的 Chromedriver 错误

java - 通过不稳定的网络同步本地数据库和远程应用程序

java - 错误java :63: reached end of file while parsing

java - BigDecimal += (add and assign) ...该怎么做?

python - 如何在同一个 docker-compose 中连接到远程 Selenium 驱动程序?