我正在使用下面的代码来获取网页上字段的属性:-
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
andValue
are different attribute and probably there were no attribute present asValue
. HenceNull
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/