2

Is there a way to get info on whether an element exists on a page or not in Robot framework?

I would like to take one action if element exists on page, or another if element doesn't exist.

In Selenium, I would use findElements(), and that would return a list of elements, or an empty list if nothing is found.

In Robot, however, if I use Get webelements and nothing is found, test breaks with error:

ValueError: Element locator 'id=asdf' did not match any elements.
zorglub76
  • 4,852
  • 7
  • 37
  • 46

1 Answers1

7

An approach is to get a boolean is the element in the page:

${present}=    Run Keyword And Return Status    Page Should Contain Element   ${your_locator}

, and then control the flow based on the value:

Run Keyword If    ${present}    The keyword(s) if present
                  ...  ELSE     The keyword(s) if NOT present

Instead of Page Should Contain Element you could use Element Should Be Visible.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • 1
    I expected 'Element should be visible' to break, but it works like a charm! Thanks a lot! – zorglub76 May 10 '17 at 13:32
  • It does "break" - in the sense that it will fail if the element is not present, but that's what the `Run Keyword If` is for - [it suppresses the normal pass/fail statuses of the called keywords, and returns a boolean instead](http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Return%20Status) - exactly for a flow you intend to use it for. – Todor Minakov May 10 '17 at 13:38
  • Aha, that clears it. Originally, I would set a variable to 'Get webelements', and then run keyword if this variable was this or that.. – zorglub76 May 11 '17 at 09:20