- Trending Categories
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
- Related Articles
- How to get selected option using Selenium WebDriver with Java?
- Get page title with Selenium WebDriver using Java.
- How to get all options of a dropdown using Python Selenium webdriver?
- How to get HTTP Response Code using Selenium WebDriver?
- How to get Response Status Code with Selenium WebDriver?
- How install Selenium Webdriver with Python?
- How to deal with ModalDialog using selenium webdriver?
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to Select date from a datepicker with Selenium Webdriver using Python?
- How to refresh a webpage using Python Selenium Webdriver?
- How to scroll down using Selenium WebDriver with Java?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How to scroll to element with Selenium WebDriver using C#?
- How to get Firefox working with Selenium WebDriver on Mac OSX?
- How to get the index of selected option in Tkinter Combobox?

Advertisements