Regular expression to create xpath with Robot Framework

I'm automating an application with the Robot Framework (Python 2.7). I use the xpath to locate the objects. I have seen that the xpath of the objects is different when I change the language application. For example:


// * [@ id = "dnn_ctr489_ViewINT_Cupones_ibtnAdd"]


// * [@ id = "dnn_ctr644_ViewINT_Cupones_ibtnAdd"]


// * [@ id = "dnn_ctr583_ViewINT_Cupones_ibtnAdd"]

This forces me to redo the Test Case because the xpath is different.

Is it possible to use a regular expression to form the xpath? How can I avoid having to redo the test cases for each different language?

Thank you. Marta

2 answers

  • answered 2018-01-12 09:03 that_techgirl

    You can use a variable file as input and put all your xpath values in it. I am using JSON in my example.Robot framework JSONlibrary can be used to parse JSON file (


        "Languages": {
             "ES": {
                "xpath1": "// * [@ id = "dnn_ctr489_ViewINT_Cupones_ibtnAdd"]",
             "FR": {
                "xpath1": "// * [@ id = "dnn_ctr644_ViewINT_Cupones_ibtnAdd"]",
             "EN": {
                "xpath1": "// * [@ id = "dnn_ctr583_ViewINT_Cupones_ibtnAdd"]",

    you can pass values to test cases based on your language selection in application by using a variable something like following

    language_selected = 'EN'

  • answered 2018-01-12 09:03 Bryan Oakley

    XPath 1

    xpath 1.x doesn't support wildcards or regular expressions. However, xpath supports several string functions which can be combined to match what you want. For example, there's a starts-with function. There's no ends-with, but there is a contains, so you could use something like this:

    //*[starts-with(@id, 'dnn_ctr') and contains(@id, 'ibtnAdd')]

    Of course, that will match somethign like "dnn_ctr blah ibtnAdd blah". If you need precisely "starts with cnn_ctr" and "ends with ibtnAdd" you can do that with a little more work:

    //*[starts-with(@id, 'dnn_ctr') and substring(@id, string-length(@id) - string-length('ibtnAdd') + 1) = 'ibtnAdd' ]

    XPath 2

    With xpath version 2 (which not all browsers support at the time I write this) you can use ends-with, making for a much more readable expression:

    //*[starts-with(@id, 'dnn_ctr') and ends-with(@id, 'ibtnAdd')]

    You can also match against a regular expression with matches:

    //*[matches(@id, 'dnn_ctr.*ibtnAdd')]