question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is the locator.count() function supposed to wait for elements matching that locator before returning the count value?

See original GitHub issue

Hi,

I’ve been trying to use the locator.count function in my typescript Playwright test and I’ve been fumbling around trying to figure out why I was getting a 0 value returned from the count function. As I was debugging, I searched with the exact locator that my test was using on the chromium dev tools and it was finding 5 elements.

The original code I was using was:

const numberOfDresses: number = await this.page.locator(DressesPage.dressesPrices).count();

which was returning 0.

And I had to change it to this in order for it to work:

await this.page.locator(DressesPage.dressesPrices).nth(1).waitFor();
const numberOfDresses: number = await this.page.locator(DressesPage.dressesPrices).count();

So it got me wondering… Is the locator.count() function supposed to wait for elements matching that locator before returning the count value? Or have I stumbled across a bug?

Thanks, Mathew

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
mathewr94commented, May 26, 2022

In the end, I just used what I was using:

await this.page.locator(DressesPage.dressesPrices).nth(1).waitFor();
const numberOfDresses: number = await this.page.locator(DressesPage.dressesPrices).count();

This question was really raised because I didn’t understand how .count() works and I thought I might’ve encountered a bug in the function. For anyone who might find this from a Google search, just know that the .count() function will get a count without waiting for any elements that match your provided locator, so you have to wait first.

1reaction
mxschmittcommented, Dec 9, 2022

Feel free to file a feature request for it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Playwright how to wait for locator that matches multiple ...
I'm storing page elements as locators in the Page Object Model, and you seemingly cannot access the selector of a locator, meaning the...
Read more >
Locator - Playwright
The method finds all elements matching the specified locator and passes an array of matched elements as a first argument to pageFunction ....
Read more >
Page Object Model (POM) With Page Factory | Selenium Tutorial
This in-depth tutorial explains all about Page Object Model (POM) With Pagefactory using examples. You can also learn implementation of POM ...
Read more >
findElement vs findElements in Selenium - BrowserStack
The findElements command returns an empty list if no elements are found using the given locator strategy and locator value.
Read more >
Find Element and Find Elements in Selenium - Tools QA
The following are the locator strategies we can use while locating the elements. Locator, Description. id, finds elements by ID attribute. The ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found