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

Question:

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

Asked By: Tal Angel

||

Answers:

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()
Answered By: Raju
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.