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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:35:36  来源:igfitidea点击:

Cannot convert from WebElement to List<WebElement>

javagenericsseleniumwebdriver

提问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 findElementsto find the list of WebElements. See API doc here

您应该使用findElements来查找 WebElements 列表。在此处查看 API 文档

findElementreturns singleWebElement whereas findElementsis 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());