Dropdowns

Dropdowns are one of the general elements present in any webpage, after buttons and textbars dropdowns are more frequently available element.

Selenium python provides Select class, with help of select class we can handle dropdowns on the webpage
Methods Present in Select Class in selenium python
  • select_by_index(int index)
  • select_by_value(String value)
  • select_by_visible_text(String text)
  • options()
  • first_selected_option()
  • all_selected_options()
  • deselect_by_index(int index)
  • deselect_by_value(String value)
  • deselect_by_visible_text(String text)
  • deselect_all()
Dropdown are formed using select tag in html, if dropdown is not formed with select tag you cannot use this Select Class methods present in python selenium

Dropdowns

select_by_index(int index)

Selects a option that have a value matching the argument. throws NoSuchElementException If there is no option with specified value in SELECT


dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.select_by_index(5)		

select_by_value(String value)

Selects a option that display text matching the argument.


dropdown= Select(driver.find_element_by_id('id_of_element'))
dropdown.select_by_value('foo')		

select_by_visible_text(String text)

Select all options that display text matching the argument.


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.select_by_visible_text('element_text')		

options()

Returns a list of all options belonging to this select tag


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.options()	

first_selected_option()

The first selected option in this select tag (or the currently selected option in a normal select)


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.first_selected_option()		

all_selected_options()

Returns a list of all selected options belonging to this select tag


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.all_selected_options()		

deselect_by_index(int index)

Deselect the option at the given index. This is done by examing the “index? attribute of an element, and not merely by counting.

Python throws NoSuchElementException If there is no option with index in SELECT


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.deselect_by_index('element_index')		

deselect_by_value(String value)

De-selects a option which matches with the given value


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.deselect_by_value('element_value')		

deselect_by_visible_text(String text)

De-selects a option which matches with the given visible text


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.deselect_by_visible_text('element_text')		

deselect_all()

Clear all selected entries. This is only valid when the SELECT supports multiple selections. Python Selenium throws NotImplementedError If the SELECT does not support multiple selections


dropdown = Select(driver.find_element_by_id('id_of_element'))
 
dropdown.deselect_all()		

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group