Course Content
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Selenium Select class

The Select class allows you to work with dropdowns on web pages. To use the Select class in Selenium, you first need to import it from the following module

selenium.webdriver.support.ui

The Select class allows you to select an element from the dropdown list. The main methods of the class are as follows: 

  • select_by_value(): select an option by passing the value attribute
  • select_by_index() : select an option by passing the index value.
  • select_by_visible_text(): select an option by passing the text you see on the page. The index value ranges from 0 to (total options – 1)

Example

# Dropdown List Test Example
# www.TestingDocs.com 

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

class DropDownTest(unittest.TestCase): 
    
    def setUp(self):
        self.driver = webdriver.Firefox()
        
        
    def test_method(self):
        driver=self.driver 
        driver.get("https://testingdocs2016.blogspot.com/2024/03/dropdown-list.html")
        driver.implicitly_wait(30)
        dropdown = Select(driver.find_element(By.NAME,"cars"))
        dropdown.select_by_visible_text("BMW")
        print(dropdown.first_selected_option.text)
        
    def tearDown(self):
        self.driver.quit()
        
            
if __name__ == '__main__': 
    unittest.main()
Join the conversation