Issue in getting count of elements using 'By.xpath'.size()

I am trying to get the count of elements using below script:

WebDriver driver = DriverFactory.getWebDriver()
def eleCount = driver.findElements(By.xpath(“//div[contains(@class,‘park w-100 p-2 ng-star-inserted’)]”)).size()
println eleCount

I have verified the xpath and returns count as 6, but when I am using it in script above it returns 0. Attached screenshot for reference.

@Brandon_Hein

Try CSS?

image

def eleCount = driver.findElements(
 By.cssSelector("div.park.w-100.p-2.ng-star-inserted")).size()

WebDriver driver = DriverFactory.getWebDriver()
def eleCount = driver.findElements(By.cssSelector(“div.park.w-100.p-2.ng-star-inserted”)).size()
println eleCount

Console view:
- 18: eleCount = ng-star-inserted")).size()
- 19: println(eleCount) 0

Still the count is coming as 0

What does this produce in the browser console?

document.querySelectorAll("div.park.w-100.p-2.ng-star-inserted").length

By this, did you use the Inspection Tool of the browser or just visually inspection ? Could the elements be inside an iframe ?

1 Like

Below is what i used to verify.

Two things…

  1. Try my console script. Let me know the result.

  2. Is your code verifying that the page is ready?

It gives count as 6 in browser console as well.

document.querySelectorAll(“div.park.w-100.p-2.ng-star-inserted”).length
6

My guess, the page is not ready when your test code tries to verify the count.

1 Like

@ThanhTo

Thank you for your input, it gave me a clue to find the embedded element path.

@Russ_Thomas

Again thank you for your css. It’s so powerful, even i am starting to use it more often and moving away from xpath :grinning:

1 Like