Robot Framework - check if element defined by xpath exists

20,948

Solution 1

By locating the element using xpath, I assume that you're using Sselenium2Library. In that lib there is a keyword named:

Page Should Contain Element which requires an argument, which is a selector, for example the xpath that defines your element.

The keyword failes, if the page does not contain the specified element.

For the condition, use this:

${Result}= Page Should Contain Element ${Xpath} Run Keyword Unless '${RESULT}'=='PASS' Keyword args*

You can also use an other keyword: Xpath Should Match X Times

Solution 2

I prefer using Get Matching XPath Count since the accepted answer throws error if element is not found

${count}= Get Matching XPath Count ${Xpath}
Run Keyword And Return If   ${count} > 0 Keyword args*
Share:
20,948
Petr Bečka
Author by

Petr Bečka

Student and part-time C/C++ developer interested in developing real time and web applications.

Updated on April 16, 2021

Comments

  • Petr Bečka
    Petr Bečka about 3 years

    I'm wondering, I'd love to find or write condition to check if some element exists. If it does than I want to execute body of IF condition. If it doesn't exist than to execute body of ELSE.

    Is there some condition like this or is it necessary to write by myself somehow?