During my Python & Selenium self-learning I came across a blocker while trying to access and select elements of a drop down.
Problem statement –
As a traveler, I want to select Business class for my flight on the Air NewZealand website , just to remind myself how atrociously expensive flying business class is .
Initial analysis –
Using Chrome Dev tools, I inspected the drop down and was expecting the various drop down entries to each have a unique id/index , so that I can select “business class” directly.
But lo-behold, the drop-down element is a <select> HTML that I had not worked with before. And each option was a <option> tag nested under the <select> tag.
Potential ways I thought to solve this –
- Find the drop down using it’s id (
driver.find_element_by_id) and find a way to iterate through the options based on their text . Likely to be brittle if the option text changes
- Directly find the XPath of the option that I want and click it .
On further googling however, I found that the Selenium library offered an out of the box class to handle <select> web elements ! 🙂
This class has some handy dandy methods to retrieve all the options under the select tag , select them by value or index etc.
- Select the option at the given index. This is done by examing the “index” attribute of an element, and not merely by counting.
- index – The option at this index will be selected
throws NoSuchElementException If there is no option with specisied index in SELECT
- Select all options that have a value matching the argument. That is, when given “foo” this would select an option like:
- value – The value to match against
throws NoSuchElementException If there is no option with specisied value in SELECT
My solution –
Here is a simple Python script the uses the Select class and some of it’s method to solve the problem above.