0

Recently I am attempting doing automation test in python with selenium-robot framework in two different browsers, but I am having an error when i try log the Alias of a Browser. The error says: "Dictionary '@{alias}' has no key '1'".

The code is this on:

*** Settings ***
Library  SeleniumLibrary

*** Variables ***
${url}  http://google.com
${browser}  chrome

*** Test Cases ***
TC to demostrate Browser Operation Keywords in Robot Framework
    [Documentation]  TC to demostrate Browser Operation Keywords in Robot Framework

    Open Browser  http://google.com  Chrome  alias=ChromeRCV

    Maximize Browser Window

    Open Browser  about:blank  ff  alias=RCVFF

    &{alias}  Get Browser Aliases

    Log  @{alias}[1]

    @{browser_ID}  Get Browser Ids

    Log  @{browser_ID}[1]

    Switch Browser  1

    Input Text  //*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input  RCVAcademy

    Sleep  4s

    Switch Browser  @{alias}[1]

    Go To  http://salesforce.com

    Close All Browsers

2 Answers 2

0

This is due to how Robot interprets the variables. Since you have written @{alias}[1] Robot assumes that the value on index 1 of list alias is also a list. To avoid this issue in future, think first what you want to get out from the variable before assigning the $, @ or &. You can see better explanation in the Robot Framework User Guide.

Consider the below example

&{DICT}=    {"key1": "value1", "key2": "value2"}

# Get a list of keys
@{KEYS}=    @{DICT.keys}
# ==> @{KEYS}= ["key1", "key2]

# Get a value from dictionary
${VALUE}=    ${DICT}[key1]
# ==> ${VALUE}= value1

# Get a value from list
${VALUE}=    ${LIST}[0]
# ==> ${VALUE}= key1

As you can see, only when returning a list value, are we using the @ sign. This is because whenever using the @ sign, Robot will interpret the given value as a list of multiple values and attempt to pass each value separately - This is also why normally lists are passed with $ as arguments to make Robot pass them as single variable instead of multiple separate values.

Additionally you are attempting to return a value from a dictionary with a list-like index. This will not work that way - You'll either have to access the value based on the key or then as a list of keys or values.

Since you only need the key to access the browser with the alias, you might as well simply use the following

Log    ${aliases.keys()}[1]

# or to log everything in the dictionary

Log    ${aliases}
4
  • "you have written @{alias}[1] Robot assumes that the value on index 1 of list alias is also a list." that's simply not true. Using @ in front of a dict variable will actually ask RF to return the dict's keys, and then you'll target a specific one - the integer 1 in this case. Sep 3, 2021 at 15:21
  • That method does not work at least on the newest RF (4.1) running on Python 3.9.7 - I double-checked and still that returns only with the error Dictionary '@{alias}' has no key '1' after creating dictionary with 2 key value pairs and attempting to log from index 1. @Todor Minakov
    – Morkkis
    Sep 5, 2021 at 16:04
  • dictionaries don't have indices, that's a property lists have; dictionary have key-value pairs. If you create a dict item which key is 1, then you'll be able to retrieve it. Sep 5, 2021 at 16:53
  • Exactly, that's why its still true that using @{dictionary}[key] will attempt to return a list value from that mentioned key - Having a non-list value on the key will return error Value of variable -- is not list or list-like. Using the @{dictionary} does return all the keys, but since the variable is still a dictionary, if you wish to access any value behind it that value must be list-like when using @. Anyway, the question seemed to be about accessing a dictonary key like a list and that's what I answered to.
    – Morkkis
    Sep 6, 2021 at 6:53
0

It appears you have misunderstood the return value of the Get Browser Aliases - it returns a dict where the key is the alias you have given to an instance, and the value is the index.

Thus when you have typed [1] you are targeting the alias 1 (integer), but that's not one of the aliases you've set - and thus it's not a key in that dictionary, and thus - the error you got.

Change to this - the "ChromeRCV" is an alias you actually have, and it should work:

Log  @{alias}['ChromeRCV']

Your Answer

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

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