Trying to set a variable in an xpath that I can then replace the text in for a dropdown menu. Not sure if there is a better way to do this I have tried "Replace Variables" also from the String library but also fails. Any help would be appreciated.


list_item = "xpath=//*[@id='testsection']//*[contains(concat(' ', @class, ' '), ' list-element ')][normalize-space(.) = '${REPLACETHIS}']"


Generate Locator For
    [Arguments]  ${locator}    ${REPLACETHIS}
    ${new_locator}=   Evaluate    ${locator}.format('${REPLACETHIS}')
    [Return]  ${new_locator}

    ${new_locator}=    Generate Locator For    ${list_item}    default
    log to console  ${new_locator}


Evaluating expression 'xpath=//*[@id='test-section']//*[contains(concat(' ', @class, ' '), ' list-element ')][normalize-space(.) = '${REPLACETHIS}'].format('default')' failed: SyntaxError: invalid syntax (<string>, line 1)

1 Answer 1


You can use the built-in variable syntax of Robot Framework to set variables in your XPath expression. Here is an example:

${REPLACETHIS}=  Set Variable  default
${list_item}=  Set Variable  xpath=//*[@id='testsection']//*[contains(concat(' ', @class, ' '), ' list-element ')][normalize-space(.) = '${REPLACETHIS}']

Then you can use the Replace Variables keyword from the String library to replace the variable in the list_item locator:

${new_locator}=  Replace Variables  ${list_item}

Alternatively, you can use the Set Test Variable keyword to set the variable and use it in your Generate Locator For keyword:

Set Test Variable  ${REPLACETHIS}  default

Generate Locator For
    [Arguments]  ${locator}
    ${new_locator}=  Replace Variables  ${locator}
    [Return]  ${new_locator}

${list_item}=  Set Variable  xpath=//*[@id='testsection']//*[contains(concat(' ', @class, ' '), ' list-element ')][normalize-space(.) = '${REPLACETHIS}']
${new_locator}=  Generate Locator For  ${list_item}

I hope this helps!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.