How to select a specific option from drop-down list?
FollowIt happens quite often to extract data from a drop-down list. Sometimes you need to get all the options on the list. Sometimes, you might just need one or several specific options. This tutorial will show you how to select whichever option on the list.
In short, writing the correct XPath is the fastest way to locate the right option. Let's use an example to show you how to do it.
You may want to use this example link to follow through:
https://www.zazo.de/eliquids/zazo/493/zazo-5-1-gratis-paket-10ml-flaschen
Here is a drop-down list and it contains a lot of options.
Let's loop through all the options in the dropdown menu first
- Click on the dropdown menu and choose Loop through the options in the dropdown menu on the tips panel
You will see the default XPath of the Loop item is //form[@class="buybox--form"]/div[1]/div[1]/div[1]/select[1]/OPTION
As you can see, there are 299 items inside the drop-down list.
To suit our needs, we need to modify the XPath for the Loop Item.
1. Choose a specific option by its index
For example, if we want to select the 5th option which is "Ananas 12mg", the correct XPath should be :
//form[@class="buybox--form"]/div[1]/div[1]/div[1]/select[1]/OPTION[5]
Just adding [X] to the end of the XPath to choose the option you want. If you replace the default XPath with the new one, you will see the 5th option appear.
2. Choose a specific option by its text
If we want to select all options containing "Banana", the correct XPath should be:
//form[@class="buybox--form"]/div[1]/div[1]/div[1]/select[1]/OPTION[contains(text(),'Banana')]
Using the syndex "contains" can help you to select the option that contains specific text.
3. Choose a specific option by its position
If we want to select all the options except the 1st one, the correct XPath should be:
//form[@class="buybox--form"]/div[1]/div[1]/div[1]/select[1]/OPTION[position()>1]
We could use ">","=",'<' after “position()” to adjust according to our needs.
If we want to select only the last option, the right XPath is:
//form[@class="buybox--form"]/div[1]/div[1]/div[1]/select[1]/OPTION[last()]
Author: Joy
Editor: Yina