Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Variable accessing error after upgrading the Python and Robot framework version

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}
like image 934
nithya a Avatar asked Apr 25 '26 12:04

nithya a


2 Answers

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}]
like image 182
Bryan Oakley Avatar answered Apr 28 '26 11:04

Bryan Oakley


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}
like image 37
Manish Kumar Avatar answered Apr 28 '26 11:04

Manish Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!