Selenium Webdriver - Select Support

Selenium can handle static dropdowns with the help of the Select class. A dropdown is identified with select tagname and its options are represented with the tagname option. The statement - from selenium.webdriver.support.select import Select should be added to work with Select class.
The methods under the Select class are listed below −
select_by_visible_text (arg)
It shall select all the options which displayed text matches with the argument.
The syntax for selecting options displaying text matches is as follows −
sel = Select (driver.find_element_by_id ("name")) sel.select_by_visible_text ('Visible Text')

select_by_value (arg)
It shall select all the options having a value that matches with the argument. The syntax for selecting all options having matching value as per the argument is as follows −
sel = Select (driver.find_element_by_id ("name")) 
sel.select_by_value ('Value')

select_by_index (arg)
It shall select an option that matches with the argument. The index begins from zero.
The syntax for selecting the option having matching value as per the argument is as follows −
sel = Select (driver.find_element_by_id ("name")) 
sel.select_by_index (1)

deselect_by_visible_text (arg)
It shall deselect all the options which displayed text matches with the argument.
The syntax for deselecting all options having matching value as per the argument is as follows −
sel = Select (driver.find_element_by_id ("name")) 
sel.deselect_by_visible_text ('Visible Text')

deselect_by_value (arg)
It shall deselect all the options having a value that matches with the argument.
The syntax for deselecting all options having matching value as per the argument is as follows −
sel = Select (driver.find_element_by_id ("name")) sel.deselect_by_value ('Value')

deselect_by_index(arg)
It shall deselect the option that matches with the argument. The index begins from zero.
The syntax for deselecting an option having matching value as per the argument is as follows −
sel = Select(driver.find_element_by_id ("name")) sel.deselect_by_index(1)

all_selected_options
It shall yield all the options which are selected for a dropdown.
first_selected_option
It shall yield the first selected option for a multi-select dropdown or the currently selected option in a normal dropdown.
options
It shall yield all the options available under the select tagname.
deselect_all
It shall clear all the selected options in a multi-select dropdown.
The code implementation for handling static dropdowns with Select class is as follows −
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://the-internet.herokuapp.com/dropdown")
#object of Select
s= Select(driver.find_element_by_id("dropdown"))
#select option by value
s.select_by_value("1")
The output shows that the option “Option 1” gets selected in the dropdown.

    46 Lectures 
     5.5 hours 

 Aditya Dua

    296 Lectures 
     146 hours 

 Arun Motoori

    411 Lectures 
     38.5 hours 

 In28Minutes Official

    22 Lectures 
     7 hours 

 Arun Motoori

    118 Lectures 
     17 hours 

 Arun Motoori

    278 Lectures 
     38.5 hours 

 Lets Kode It
 Print
 Add Notes
 Bookmark this page