user1053540
user1053540

Reputation: 39

Cannot convert from WebElement to List<WebElement>

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...

Upvotes: 2

Views: 15471

Answers (1)

Saifur
Saifur

Reputation: 16201

You should use findElements to find the list of WebElements. See API doc here

findElement returns single WebElement whereas findElements is plural and should be the expected one in this case.

List<WebElement> fields = driver.findElements(By.xpath("//input[@type='text']"));
System.out.println(fields.size());

Upvotes: 11

Related Questions