zorglub76
zorglub76

Reputation: 4942

Run robot keyword depending on existence of the element

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.

Upvotes: 2

Views: 7788

Answers (1)

Todor Minakov
Todor Minakov

Reputation: 20077

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.

Upvotes: 9

Related Questions