Dropdowns are one of the general elements present in any webpage after buttons and text bars dropdowns are a 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
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
Selects an option that has a value matching the argument. throws NoSuchElementException If there is no option with the specified value in SELECT
dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.select_by_index(5)
Selects an option that displays text matching the argument.
dropdown= Select(driver.find_element_by_id('id_of_element'))
dropdown.select_by_value('foo')
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')
Returns a list of all options belonging to this select tag
dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.options()
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()
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 the option at the given index. This is done by examing the "index" attribute of an element and not merely by counting.
Python throws
dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.deselect_by_index('element_index')
De-selects an option that matches with the given value
dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.deselect_by_value('element_value')
De-selects an option that matches with the given visible text
dropdown = Select(driver.find_element_by_id('id_of_element'))
dropdown.deselect_by_visible_text('element_text')
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()
Dependent and Independent Xpath
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.