1

I'm struggling to create a test case in robotframework using Selenium. Basically my goal is capture some web elements in FOR loop using some xpath query, checking the actual web elements exist on the page, in case doesn't pass next. I've created following script:

*** Test Cases ***
FOR     ${href}     IN      @{hrefs}
        Log     ${href}
    ${pl}=       Run Keyword And Continue On Failure    Get Element Count        xpath://a[contains(@href,'test')]
        ${photo_link}=    Run Keyword If    ${pl}> 0 Get WebElement    xpath://a[contains(@href,'test')]
            ${test_a}=    Set Variable    ${photo_link.get_attribute('innerHTML')}
    ELSE
        ${test_a}=  do something else
END
    Close All Browsers

But I always get error:

'Else' is a reserved keyword. It must be in uppercase (ELSE) when used as a marker with 'Run Keyword If'.

Checking documentation I was not able to find any solution. Am I using wrong syntax? Is there some alternative way to avoid and skip "Get WebElements" without match? Thank you

1 Answer 1

3

There are a couple syntax errors, the framework parser throws the exception for one of them - you'll see the other when this one is fixed. Starting from "the hidden error" , in this piece here:

Run Keyword If    ${pl}> 0 Get WebElement

, there is no delimiter (2 or more spaces) between the condition ${pl}>0 and the action when true (Get Webelements). It should be

Run Keyword If    ${pl}> 0    Get WebElement

The error you see is from the usage of the reserved keyword ELSE; in general it is for stipulating the action when the condition is false, and is part of the call to Run Keyword If, e.g. it should be written as:

Run Keyword If     condition    Action If True    ELSE    Action If False

or like this - with 3 dots, for continuation - when written on multiple lines:

Run Keyword If     condition    Action If True    
...    ELSE    Action If False

You lack that "continuation" (a term I made up on the spot, don't quote me on it :), the user guide was using something else) - it's a free standing & independent line, thus the error.


For fixing it, you'd better use the new IF/ELSE blocks introduced in version 4 of the framework; it would look like something like:

IF    ${pl}> 0
    ${photo_link}=    Get WebElement    xpath://a[contains(@href,'test')]
    ${test_a}=    Set Variable     ${photo_link.get_attribute('innerHTML')}
ELSE
    ${test_a}=    do something else
END

It looks almost the same as your code, but doesn't use Run Keyword If. And in RF v3.x by using the keyword you would have to use Run Keywords to use multiple keywords in the true blcok, and - you cannot do variable assignment within Run Keywords. What you want to achieve can be done there, with a sligly different flow and will look a bit awkward, but the new IF/ELSE syntax is your safest bet.

Sign up to request clarification or add additional context in comments.

3 Comments

The example with the new syntax needs an END after the ELSE.
Thanks @BenceKaulics (you should've straight up edited it :)
Yes, I should have, but I do not like editing fresh answers, they tend to change a lot. :)

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.