I want to count some tags in this page Link
I am trying to count the tags of opening positions so, I tried this code using java to count but I always find my count is = 0;
public By cardsNumOfPositions = By.xpath("//div[@class='card']");
List<WebElement> element = driver.findElements(cardsNumOfPositions);
int countelements = element.size();
And I write this function to count:-
public void printCountElements() {
System.out.println(countelements);
}
Everytime the count is 0, I searched for an Iframe but I didn't find any. so how can I get the size of this element?
CodePudding user response:
Also, you can make countelements
a static variable.
like this:
static int countelements;
now since it's static, its value will persist. You can call it like this:
public void printCountElements() {
countelements = element.size();
System.out.println(countelements);
}
CodePudding user response:
Looking at the code, it doesn't look like there's anything wrong with it if what you're trying to achieve is to count the number of elements on the page matching that xpath.
The xpath also looks good, and I've tested it and it works. I can see that iframes exist on the page, but it doesn't look like these elements are within those iframes, so it doesn't look like stepping into an iframe would be required.
Are there sufficient waits in place, to make sure these elements are fully loaded before we're trying to find them? I'm worried that we're trying to put these elements into a list before they've loaded.
Failing that, what I'd check next is run the same test, but this time using the parents (or parents parents) of this element, and follow it up the chain to see if we can get any hits whilst running the code.