Check multiple locators using or condition and navigate to different flow in robot framework

Hello All,

I have written robot script. At one point in my application one page appears intermittently. My code is as below-
${present}= Run Keyword And Return Status Wait Until Element Is Visible ${xpath1} 60s
Log ${present}
Run Keyword If ${present} Select link accept with certificate

I always polls for this page element ${xpath1} in my script and if visible then only click on that link. It wait for 60s.

I want to check two locators at a time e.g.
${xpath1} is not visible then check for other element ${xpath2} at the same time.

If xpath1 is visible I have to perform different action without wait for 60s and
if xpath2 visible I have to perform different action without wait for 60s

Either of xpath1 or xpath2 always visible.

Is there any good suggestion for this?

Hi @bk-user ,

when you have more complex logic like this it’s probably best to move it to it’s own keyword which then gives you some more options.

For example if you create a keyword like this:

Do Page Checks
    ${present_x1}= Run Keyword And Return Status Wait Until Element Is Visible ${xpath1} 1s
    Log ${present_x1}
    Run Keyword If ${present_x1} Select link accept with certificate
    ${present_x2}= Run Keyword And Return Status Wait Until Element Is Visible ${xpath2} 1s
    Log ${present_x2}
    Run Keyword If ${present_x2} Select other link
    IF    ${present_x1} or ${present_x2}
        Pass Execution
    ELSE
        Fail

Basically if either item is found, return a pass, otherwise if neither were found, make the keyword fail so that you can detect if either item was found and dealt with.

Then you can use Wait Until Keyword Succeeds to re-run that keyword till it either passes or time is up (this is why the timeouts were reduced to 1s)

Wait Until Keyword Succeeds    60 sec    2 sec    Do Page Checks

Obviously you can adjust this to suit your needs, but this should give you the general idea.

Dave.

1 Like

@damies13

What if this condition is true but somehow page taken time to load hence did not find ${xpath1} visible. So expectation was, it should click on Select link accept with certificate. But as we added timeout 1s, it will move to next step and check other flow which will fail our scenario?

${present_x1}= Run Keyword And Return Status Wait Until Element Is Visible ${xpath1} 1s
Log ${present_x1}
Run Keyword If ${present_x1} Select link accept with certificate

At the same time can we check for both the ${xpath1} and ${xpath2} and get the status of which xpath is visible first then navigate to that flow?

Hi @bk-user ,

Within the keyword example I gave you, yes it will only wait for xpath1 or xpath2, if both are false then the keyword fails.

That’s where Wait Until Keyword Succeeds it will rerun our keyword until the it either passes (xpath1 or xpath2 was true) or we reach the timeout condition of 60 sec.

We can’t actually wait 60sec for both conditions in parallel, so what I showed you was that we can wait 1 sec for each condition sequentially, if both fail we repeat that process until either passes or we waited long enough.

with this method In effect we’re waiting for either condition in parallel buy checking both with a short timeout on each check and then repeating.

Hope that makes things clearer,

Dave.

1 Like