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?
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)