8

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?

3 Answers 3

30

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
Sign up to request clarification or add additional context in comments.

6 Comments

One (relatively minor) downside to this solution is that it runs the run-on-failure hook which is 'Capture Page Screenshot' by default, wasting time and disk space.
@MariusGedminas, so do you have another solution?
I use this solution, but I wrap it with ${fail keyword}= Register keyword to run on failure No operation and Register keyword to run on failure ${fail keyword}.
@MariusGedminas bit confused how the wrapping would work, so what you would have done is - ${fail keyword}= Register keyword to run on failure No operation ${present}= Run Keyword And Return Status Element Should Be Visible id=my-element Run Keyword If ${present} My Cool Keyword Register keyword to run on failure ${fail keyword}, is this right?
Is it possible to do it in one line? Like IF KeywordReturningElementStatus My Cool Keyword *** Keyword *** KeywordReturningElementStatus ${status}= Run Keyword And Return Status Element Should Be Visible id=my-element [Return] ${status}
|
3

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}

Comments

3

Here's a faster and less wordy alternative:

${c} =   Get Element Count   ${locator}
Run Keyword If   ${c}>0    Call Your Keyword Here

2 Comments

But this won't check visibility, only presence in the DOM, right?
Right-o, YMMV... ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.