You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Locators do not auto-wait when resolved to multiple elements (Strict mode) #30363
Execute a click and provide a locator that matches more then one element
Expected behavior
Test fails because of Strict mode violation, but only after 5000ms as Playwright awaits to see if the page changes and makes the action eligible.
This should be similar to when trying to click an element and using a locator that does not match any element. Playwright doesn't fail immediately but wait.
I'm not sure whether it's a bug, but either way I see no reason not to "auto-wait" in the case of strict mode violation. The same arguments that support auto waiting in general would apply to when strict mode is violated.
That's not the same as toBeVisible() though right? Anyway I agree with you that we can work around it and get the same results, I just think that auto-wait should be applicable to strict mode in general. I gave the example of an assertion but it's also correct using actions
There are a few use-cases but mainly it reduces flakiness. For example, I have a form with two "Edit" buttons that edit different sections, then when I click "Next" I get to another form with a single "Edit". So the flow is
Click Next
Click Edit.
But because the first form has two edit buttons, there will be a race condition of whether the "click Edit" code runs after the new content is loaded.
Again I can add an assertion to count edit buttons as you mentioned, but the same statement is true to any use-case that requires "auto-await". So I do think that strict mode should also "auto-wait"
But because the first form has two edit buttons, there will be a race condition of whether the "click Edit" code runs after the new content is loaded.
Even if Playwright was auto-waiting for the locator to resolve to just one element, it could fail in this case if there was only one "Edit" button on the first page as well. A more general solution to this problem would be to wait for the new page to load, e.g. by constructing a more specific locator for the "Edit" button or by waiting for another UI element to show up on the new page before trying to click edit.
Yep, and in that case you would need to find something more specific. My feedback here is that we encountered multiple cases in that nature (assertions or expects) where strict mode had a race condition and auto-wait would have solved our issue.
There are always workarounds, but the beauty of Playwright to me is that the auto-waiting / polling and other features make it more natural to write tests without needing to think through flakiness and race conditions.
I'll leave it for the PW team to decide the priority of this :) but from my perspective, I do think it'll provide a worthwhile improvement to the dev experience.
Reacted by Yury Semikhatsky, Chad Sheets, gchust and Bryon Larrance
We encounter this issue when dismissing dialogs. Running locally the dialog dismiss (+animation) is fast enough to avoid a strict mode error but in CI we'll encounter flakey failures if the dialog doesn't dismiss fast enough.
We could solve the issue by waiting for the dialog to be removed (or for the locator to resolve to only 1 element) but it feels like the elegance of Playwright is its ability to handle this sort of situation for us so we're not having to code defensively.
Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.
Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.
Activity
yury-s commentedon Apr 15, 2024
You can wait for the locator to resolve to exactly 1 element first and only after that perform click:
Would that work?
Can you tell us more about the use case where you have multiple clickable items but then they go down to 1?
gal-checksum commentedon Apr 15, 2024
That's not the same as toBeVisible() though right? Anyway I agree with you that we can work around it and get the same results, I just think that auto-wait should be applicable to strict mode in general. I gave the example of an assertion but it's also correct using actions
There are a few use-cases but mainly it reduces flakiness. For example, I have a form with two "Edit" buttons that edit different sections, then when I click "Next" I get to another form with a single "Edit". So the flow is
But because the first form has two edit buttons, there will be a race condition of whether the "click Edit" code runs after the new content is loaded.
Again I can add an assertion to count edit buttons as you mentioned, but the same statement is true to any use-case that requires "auto-await". So I do think that strict mode should also "auto-wait"
yury-s commentedon Apr 15, 2024
Even if Playwright was auto-waiting for the locator to resolve to just one element, it could fail in this case if there was only one "Edit" button on the first page as well. A more general solution to this problem would be to wait for the new page to load, e.g. by constructing a more specific locator for the "Edit" button or by waiting for another UI element to show up on the new page before trying to click edit.
gal-checksum commentedon Apr 15, 2024
Yep, and in that case you would need to find something more specific. My feedback here is that we encountered multiple cases in that nature (assertions or expects) where strict mode had a race condition and auto-wait would have solved our issue.
There are always workarounds, but the beauty of Playwright to me is that the auto-waiting / polling and other features make it more natural to write tests without needing to think through flakiness and race conditions.
I'll leave it for the PW team to decide the priority of this :) but from my perspective, I do think it'll provide a worthwhile improvement to the dev experience.
cjsheets commentedon Aug 2, 2024
We encounter this issue when dismissing dialogs. Running locally the dialog dismiss (+animation) is fast enough to avoid a strict mode error but in CI we'll encounter flakey failures if the dialog doesn't dismiss fast enough.
We could solve the issue by waiting for the dialog to be removed (or for the locator to resolve to only 1 element) but it feels like the elegance of Playwright is its ability to handle this sort of situation for us so we're not having to code defensively.
pavelfeldman commentedon Sep 4, 2025
Why was this issue closed?
Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.
Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.