How to check if element is present or not using Playwright and timeout parameter

I need to find a specific element in my webpage. The element may be in the page or not.

This code is giving me error if the element is not visible:

error_text =
            self.ERROR_MESSAGE, timeout=7000).inner_text()

How can I look for the element using timeout, and get a bool telling me if the element is found or not?

>Solution :

You have to use the page.is_visible(selector, **kwargs) for this as this returns a boolean value. Playwright Docs,

bool = page.is_visible(selector, timeout=7000)


if page.is_visible(selector, timeout=7000):
    print("Element Found")
    print("Element not Found")

You can also use expect assertion if you directly want to assert the presence of the element and fail the tests if the condition is not met.

expect(page.locator("selector")).to_have_count(1, timeout=7000)

Leave a Reply