- 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 selected option using Selenium WebDriver with Python?
We can obtain the option selected in a dropdown with Selenium webdriver. The first_selected_option method fetches the option selected in the dropdown. Once the option is returned we need to apply a text method to get option text.
Let us consider the select dropdown Continents and fetch its selected item −
Example
from selenium import webdriver from selenium.webdriver.support.select import Select import time driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm") # identify dropdown s = Select(driver.find_element_by_xpath("//select[@name='continents']")) #select by option index s.select_by_index(4) #get selected item with method first_selected_option o= s.first_selected_option #text method for selected option text print("Selected option is: "+ o.text) driver.close()
Output
Advertisements