How to use Run Keyword If in Robot Framework



  • I want to run Keyword if Element is visible. E.g I want to check that if a button is visible on the page then run particular Keyword. Below is my code. Create button is Keyword and want to run if CreateButton is visible on the page otherwise not.

    Create Button
        Sleep    10s
        Click Element    ${CreateButton}
    
    Create Case with Subject
        Sleep    15s
        ${CreateButtonVisible} =    Page Should Contain Element    ${CreateButton}
        Run keyword If    ${CreateButtonVisible}    Create Button
    


  • It seems like you don't want to report error if element was not found. In such case, the most straight-forward way is to run the keyword and ignore errors if element is not found:

    Run Keyword And Ignore Error    Click Element    id=element_id
    

    Also, hard-coding sleep intervals can make your tests slow and flaky. Using Wait Until Page Contains is better.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2