[Question] Is the locator.count() function supposed to wait for elements matching that locator before returning the count value?
See original GitHub issueHi,
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:
- Created a year ago
- Reactions:2
- Comments:12 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In the end, I just used what I was using:
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.
Feel free to file a feature request for it!