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 `count` on Locator working as expected?

See original GitHub issue

So I have a locator for this selector here like this

const foo = '.tab-content table#outer-table > tbody > tr:not(#inner-table-row) > td'
const el = await page.locator(foo).count()
console.log(el)

I wanted to do an assertion on the number of elements retrieved, so count() seemed appropriate. However when I log out the count I see 0 and this warning

Uncaught exception: Error: strict mode violation: ".tab-content table#outer-table > tbody > tr:not(#inner-table-row) > td" resolved to 50 elements:

So the selector resolved to 50 elements:, yet the count of those elements is 0?

How can that be?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aslushnikovcommented, Jul 5, 2022

@vomc the rule of thumb is to always prefer web-first assertions!

To assert count, you should use expect(locator).toHaveCount(50)

1reaction
vomccommented, Jul 5, 2022

Ah! yes that works, thank you @ltsuda ! To me as a beginner in Playwright this was not obvious from the documentation. Since it returns a Promise, I assumed that it would get the total count once the page is loaded.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Question] Is the locator.count() function supposed to wait for ...
The original code I was using was: const numberOfDresses: number = await this.page.locator(DressesPage.dressesPrices).count();.
Read more >
Powershell .count function not working as expected
I find that if I declare the array as an empty array and then add items to it (+=)instead of assigning a result...
Read more >
COUNT and COUNTA functions to count cells in Excel - Ablebits
This short tutorial explains the basics of the Excel COUNT and COUNTA functions and shows a few examples of using a count formula...
Read more >
Telework and Remote Work Frequently Asked Questions
If an employee is authorized to work remotely from one location, can they work remotely from a different location? An employee's pay is...
Read more >
Inventory Cycle Counting 101: Best Practices & Benefits
Cycle counting is a method of checks and balances by which companies ... Counters should review the inventory locations, descriptions, ...
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