Run robot keyword depending on existence of the element

automated-tests, robotframework, selenium2library

Solution

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`.

Problem

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. ```

Original source