BruceyBandit
BruceyBandit

Reputation: 4324

How to get a count of number of WebElements

Is there a way to be able to find elements via WebElement and not using By?

Reason is that I want to find all the elements for 'testElement' but I am unsure how to code it as it is expecting a By and not WebElement. I am hoping there is a way via WebElement.

Example code below:

@FindBy(id = "test")
public WebElement testElement;

To get the size I would try the below but it fails as it expects a By:

List<WebElement> testCount = driver().findElements(elementPage.testElement);
        Assert.assertEquals(testCount.size(), 5);

Upvotes: 0

Views: 892

Answers (1)

Valga
Valga

Reputation: 459

you're declaring it as a single webelement, you should declare it as a list, like this:

@FindBy(id = "test")
public List<WebElement> testElement;

then you can use it in your code like this:

Assert.assertEquals(elementPage.testElement.size(), 5);

you can also try it directly, without the elementPage:

List<WebElement> testElement= driver.findElements(By.id("Test"));

System.out.println("Element count: " + testElement.size());

Hope it helps.

Upvotes: 1

Related Questions