Is there a way in python with selenium that instead of selecting an option using a value or name from a drop down menu, that I can select an option via count? Like select option 1 and another example select option 2. This is because it's a possibility that a value or text of a drop down menu option can change so to ensure an option is selected, I just want to say select the first option (regardless what it is) and for another example select the fifth option etc.
Below is the code I have using value to select an option which will be a problem if the value changes in the future:
pax_one_bags = Select(driver.find_element_by_id("ctl00_MainContent_passengerList_PassengerGridView_ctl02_baggageOutDropDown"))
pax_one_bags.select_by_value("2")
pax_two_bags = Select(driver.find_element_by_id("ctl00_MainContent_passengerList_PassengerGridView_ctl03_baggageOutDropDown"))
pax_two_bags.select_by_value("5")
Yes, there is select_by_index()
method:
pax_one_bags = Select(driver.find_element_by_id("ctl00_MainContent_passengerList_PassengerGridView_ctl02_baggageOutDropDown"))
pax_one_bags.select_by_index(1)
Or, you can also get the item from the options
list by index and click:
pax_one_bags = driver.find_element_by_id("ctl00_MainContent_passengerList_PassengerGridView_ctl02_baggageOutDropDown")
pax_one_bags_select = Select(pax_one_bags)
pax_one_bags.click()
pax_one_bags_select.options[1].click()