Skip to content

How do I select an option in the drop-down

This example demonstrates selecting an option from the drop-down list.

In the solution shown below, you will see that the option "New Jersey" label is selected from the "Select State" drop-down list in HICC application using the "select" command.

Option locators provide different ways of specifying options of an HTML Select element. There are several forms of Select Option Locator - label, value, id or index.

Solution:

Approach #1: Using label option locator

# Command Target Value
Select 'New Jersey' from the State drop-down using label option locator
1 select //select[@id="dpl_state"] label=New Jersey

Approach #2: Using value option locator

# Command Target Value
Select 'New Jersey' from the State drop-down using value option locator
1 select //select[@id="dpl_state"] value=object:29

Approach #3: Using id option locator

# Command Target Value
Select 'New Jersey' from the State drop-down using id option locator
1 select //select[@id="dpl_state"] id=Option 3

Approach #4: Using index option locator

# Command Target Value
Select 'New Jersey' from the State drop-down using index option locator
1 select //select[@id="dpl_state"] index=3

Tips, Tricks, Gotchas & Best Practices:  

  • The above solution will work for you if the drop-down is built as a <select> HTML tag. While this may fail if the drop-down is built using other tags, then you can refer to below alternatives:
  • The drop-down is not a native HTML drop-down but built using list tags - <ul><li></li></ul>), then you can use "click" command to expand the drop-down list and then click on the required option.
  • The drop-down does not have any attribute defined then you can use the assignId to assign an id to the tag and perform a click action either using click or runScript commands.
  • If your drop-down takes time to update after select action, then you can use the selectAndWait command.
  • You can also validate the options or the selected options. To understand more about this, click here.

Feedback and Knowledge Base