Robot Framework If element is visible execute a keyword

32,234

Solution 1

I experienced the same issue, I use this solution:

${present}=  Run Keyword And Return Status    Element Should Be Visible   id=my-element
Run Keyword If    ${present}    My Cool Keyword

Solution 2

I had similar situation and I used the script below:

${IsElementVisible}=  Run Keyword And Return Status    Element Should Be Visible   ${Element1}
Run Keyword If    ${IsElementVisible}  MyCase1  ELSE  Click Element  ${Element2}

Solution 3

Here's a faster and less wordy alternative:

${c} =   Get Element Count   ${locator}
Run Keyword If   ${c}>0    Call Your Keyword Here
Share:
32,234
PurpleBeni
Author by

PurpleBeni

QA Automation Developer.

Updated on July 09, 2022

Comments

  • PurpleBeni
    PurpleBeni almost 2 years

    I have a page that may and may not contain a certain element that will affect all the xpaths.

    I need to run a "Run Keyword If" to identify if this element exists and if so to execute another keyword.

    I tried to set the "Page Should Contain Element" and "Element Should Be Visible" as variables and pass it in the If statement but it only return None.

    What can I use to identify an element in a page?