- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements