2

I know its a common question, but bear with me, as I really have no idea how to even solve this.

I need my robotframework code to click on a dropdown list.

So the html code for the desired list on the page I am testing is as follows:

<div class="col-sm-new">
  <div class="grid-item">
    <label class="picker-option">
      <input type="radio" id="B & C" name="industry_type" value="B & C">
      <div class="item grey-bg">
        <div class="header-arrow itemname">B & C
        </div>
        <span class="itemdesc"></span>
      </div></label>

I really have no clue what locator I should be using. I tried

Click Element id=B & C

Locator not found

Select from List by Index id=B & C

Also throws a locator not found error.

2 Answers 2

2

Your first approach could work but first you should open the drop-down menu, because before that the list item is not visible or might not even exists.

Click Element    #locator of the drop-down button/list
Click Element    id=B & C

Your second approach is not working because the Select from List by Index keyword expects two parameters.

Selects options from selection list locator by indexes.

Indexes of list options start from 0.

You did not pass the locator of the list, which can be an xPath expression or CSS selector and you did not pass a valid index neither. Instead you have passed the locator of the list item.

In both cases you need the locator of the list itself first, and then you can select a list item with one of the keywords below.

  1. Select From List By Label to match by text. (<div class="header-arrow itemname">B & C </div>)

    Select From List By Label    id:listId    B & C
    
  2. Select From List By Value to match by value. (<input type="radio" id="B & C" name="industry_type" value="B & C">)

    Select From List By Value    id:listId    B & C
    

But all of this will work only if your list has <list> tag and the list items have <select> tags. Based on the library source code.

def _get_select_list(self, locator):
    el = self.find_element(locator, tag='list')
    return Select(el)

For example with a drop-down list, with simple list-item elements: <li>, the following error would occur:

Message: Select only works on <select> elements, not on <li>


This would lead back to a custom solution instead of the rarely usable Select From List ... keywords. Here is an example you can try here on SQA.

*** Settings ***
Library     SeleniumLibrary

*** Variables ***
${SE HELP LOCATOR}    //a[@class='-link js-help-button']

Select Element From Se Help Dropdown
    [Arguments]     ${item label}
    # Open drop-down
    Click Element   ${SE HELP LOCATOR}
    # Click on item to select
    Click Element   //li/a[contains(text(),'${item label}')]
    # Optionally close the drop-down here if needed
    # Click Element   ${dropdown locator}

*** Test Cases ***
Select From List Example
    [Tags]     SQA
    [Setup]    Open Browser    https://sqa.stackexchange.com/    Firefox
    Maximize Browser Window
    Set Selenium Speed      0.1

    Select Element From Se Help Dropdown    Help Center

    Location Should Be    https://sqa.stackexchange.com/help

    [Teardown]    Close Browser

Based on this you should be able to create your own keyword for your own drop-down.

0
0

Click Element //li/a[contains(text(),'${item label}')]

This worked out. Creating a keyword and using this method will solve your issue

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.