Search code examples
pythonplaywrightplaywright-python

How to check for element existence without getting an error in Playwright


I only wish to check if this element exist, or to test that it is hidden or not.

def get_error_message(self):
    is_error_msg = not self.page.wait_for_selector(
        self.ERROR_MESSAGE, timeout=5000).is_hidden()
    return is_error_msg

The line with wait_for_selector throws an error is the element is hidden. But I just want to know if the element is hidden or not, I'm not expecting it to be visible or hidden all the time


Solution

  • You could use something called element_handle.is_visible() from here.

    your code should look something like below.

    href_element = page.query_selector("a")
    href_element.is_visible()