How to get all options of a dropdown using Python Selenium webdriver?


We can obtain all options of a dropdown with Selenium webdriver inPython using the method options. It returns a list of the options in the dropdown.

Then we have to use the method text, to get the option text.

A dropdown is represented by select tag and its available options are represented by the tagname option. To handle dropdown in Selenium, we have to take the help of the Select class.

Let us see the html code of a dropdown along with its options – By Subject and By Name.

Syntax

l = driver.find_element_by_name("selType")
d = Select(l)

for opt in d.options -

m = opt.text

Example

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.tutorialspoint.com/tutor_connect/index.php")
#Select class for dropdown
l= driver.find_element_by_name("selType")
d= Select(l)
print('Options are: ')
#iterate over dropdown options
for opt in d.options:
#get option text
   print(opt.text)
#browser quit
driver.quit()

Output

Updated on: 03-Apr-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements