Python doesn't wait for checkbox

Question:

Good afternoon.

When in test you go to the site page, the next step is to uncheck the checkbox. But as soon as the page loads, the playwrite tries to immediately click (uncheck) the checkbox. Tell me how to tell the playwright to wait a while for the checkbox to be clicked. The checkbox is visible. But immediately after opening the page, it is not clicked.

If I add a timeout of 1 or more seconds everything works. But without it, no. Thanks in advance!

If you understand selenium, then a hint in which direction to think will also come in handy!

    if page.is_checked('//input[@type="checkbox"]'):
        page.uncheck('//input[@type="checkbox"]')
    else:
        raise Exception('unchecked')

When i add that, all working good.

        page.wait_for_timeout(5000)
        if page.is_checked('//input[@type="checkbox"]'):
            page.uncheck('//input[@type="checkbox"]')
        else:
            raise Exception('unchecked')
Asked By: Valtesar

||

Answers:

Instead of page.wait_for_timeout(5000), you can write an expect assertion, which has a default timeout of 5 seconds.

locator = page.locator("//input[@type="checkbox"]")
expect(locator).to_be_checked()

Or, if you want to increase the timeout you can do this:

locator = page.locator("//input[@type="checkbox"]")
expect(locator).to_be_checked(timeout=7000)
Answered By: Alapan Das