Skip to content

[Bug]: Locators do not auto-wait when resolved to multiple elements (Strict mode) #30363

Closed

Description

@gal-checksum

Version

1.43.1

Steps to reproduce

  1. Set action timeout to 5000 ms
  2. 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.

See auto waiting
https://playwright.dev/docs/actionability#introduction

Actual behavior

Playwright fails immediately.

Additional context

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.

Environment

Any

Activity

  1. yury-s commented on Apr 15, 2024

    @yury-s
    Member

    You can wait for the locator to resolve to exactly 1 element first and only after that perform click:

      await expect(locator).toHaveCount(1);
      await expect(locator).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?

  2. gal-checksum commented on Apr 15, 2024

    @gal-checksum
    Author

    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

    1. Click Next
    2. 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"

  3. yury-s commented on Apr 15, 2024

    @yury-s
    Member

    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.

  4. gal-checksum commented on Apr 15, 2024

    @gal-checksum
    Author

    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.

  5. cjsheets commented on Aug 2, 2024

    @cjsheets
    Member

    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.

  6. pavelfeldman commented on Sep 4, 2025

    @pavelfeldman
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions