Check if element is enable Playwright


I am starting with Playwright Library and I want to know how to verify if element is enable and if so, do something if not do something else.

With Selenium I manage it by doing this:

${present}=  Run Keyword And Return Status    RPA.Browser.Selenium.Element Should Be Visible   //*/span[contains(text(), '${vMenuName}')]    5s
    IF    ${present}
        accessUserPlaywright    ${employe}
        choosingSelectedMenuPlaywright    ${vMenuName}
        accessUserPlaywright    ${employe} 


There is Get Element States keyword that you can use.

I don’t understand how to work with Get Element States.

When I try:

${present}=Get Element States   MyXpath
Log To Console    ${present}

The log show me [‘detached’] whether my element is there or not.

Try this code to get a boolean whether the element is enabled or not:

${element_exists}=    Get Element States    ${selector}    then    bool(value & enabled)

You can experiment with other statuses instead of enabled as well.

More examples are available in the documentation that @linkraivo shared.

You are talking about Playwright, but in your code you refer to Selenium.
It’s not the same thing…

As I said to my first message:

“I am starting with Playwright Library[…]With Selenium I manage it by doing this”

I try ${element_exists}= Get Element States ${selector} then bool(value & enabled) or ${element_exists}= Get Element States ${selector} then bool(value & visible) and I always got the same result: False (or true with visible) even if the menu (${selector}) is there or not.

…even if the menu (${selector}) is there or not

  1. In these situations, if you use the DevTools in the browser to manually search for the XPath you are using, does it find any occurrences? If you can’t see the menu, but the robot still finds the element it could be that a) the selector isn’t specific enough or b) that the element is hidden in the HTML

  2. Is it a public page by any chance or can you share a section of the HTML so we can take a look and better help you?