java 无法从 WebElement 转换为 List<WebElement>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29606900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert from WebElement to List<WebElement>
提问by user1053540
List<WebElement> fields = (List<WebElement>) driver.findElement(By.xpath("//input[@type='text']"));
System.out.println(fields.size());
This my Code and the error is
这是我的代码,错误是
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.util.List...
线程“main”中的异常 java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement 无法转换为 java.util.List...
回答by Saifur
You should use findElements
to find the list of WebElements. See API doc here
您应该使用findElements
来查找 WebElements 列表。在此处查看 API 文档
findElement
returns singleWebElement whereas findElements
is plural and should be the expected one in this case.
findElement
返回单个WebElement 而是findElements
复数,在这种情况下应该是预期的。
List<WebElement> fields = driver.findElements(By.xpath("//input[@type='text']"));
System.out.println(fields.size());