3

Below mentioned script has been worked in old python (2.7.x) and Robotframework version . The same code is not being worked ,after upgrading the python version from 2.7 to 3.7.2 and the robot version to 3.1.1.

I'm getting this error while executing script:

Variable '${var}' is string, not list or dictionary, and thus accessing item '${var}' from it is not possible.

Code :

${loc}   xpath=(//*[contains(@class,"c3-legend-item ")])

: FOR    ${row}    IN RANGE    1        ${Count}
\    ${Exp_Name} =    Get Text    ${loc}[${row}]    
\   Log    ${Exp_Name}

2 Answers 2

6

In robot version 3.1 there was a backwards-incompatible change. From the release notes:

Square brackets after variable like ${var}[xxx] is considered item access

Syntax like ${var}[xxx] is now considered variable item access (#2601), not variable ${var} followed by a literal string [xxx]. If the latter is desired, escaping like ${var}[xxx] is needed.

In your specific case you're using ${loc}[${row}], where you're expecting [${row}] to be appended to ${loc}. In 3.1 robot thinks that [${row}] is an index into ${loc}.

The fix, as suggested in the release notes, is to escape the opening square bracket:

\    ${Exp_Name} =    Get Text    ${loc}\[${row}]
Sign up to request clarification or add additional context in comments.

Comments

1

Use catenate to build dynamic xpath. This will work for you.

${loc}   xpath=(//*[contains(@class,"c3-legend-item ")])

: FOR    ${row}    IN RANGE    1        ${Count}

\    ${dynamic_xpath}=    Catenate    SEPARATOR=    ${loc}    [${row}]

\    ${Exp_Name} =    Get Text    ${dynamic_xpath}

Comments

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.