Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.page.wait_for_selector(
            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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
print(bool)

#OR

if page.is_visible(selector, timeout=7000):
    print("Element Found")
else:
    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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading