- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
What are the methods available for handling static dropdowns in a page in Selenium with python?
There are multiple methods available for handling static dropdowns in a page in Selenium. The static dropdowns are an integral part of a web page. This type of UI elements are mostly developed for birthday or age selection on a page.
A dropdown is not an element alone. It is a group of elements. For example for selection of birth date, we have multiple options to be selected for day, month and the year. Thus the approach is to first get the primary element and then move to its sub elements for selection.
Select class is provided by the Selenium API which works with the static dropdown on the page. We need to import selenium.webdriver.support.select.Select to work with the static dropdowns having the select tags in the html code.
Syntax
Select(driver.find_element_by_tag_name("select"))
The methods under the Select class are listed below −
select_by_visible_text(args) − Selection with the help of text displayed as option.
This method is the simplest one which selects the option based on the visible text. NoSuchElementException thrown if there is no option which matches with the text given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.select_by_visible_text('Tutorialspoint')
select_by_index(args) − Selection with the help of index of option.
This method selects the option based on the index of the specific option. The index of the elements mostly start with 0. NoSuchElementException thrown if there is no index which matches with the index given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.select_by_index(1)
select_by_value(args) − Selection with the help of value of option.
This method selects the option based on the value of the specific option. NoSuchElementException thrown if there is no value which matches with the value given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.select_by_value('Selenium')
deselect_by_value(args) − Deselection with the help of value of option.
This method deselects the option based on the value of the specific option. NoSuchElementException thrown if there is no value which matches with the value given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.deselect_by_value('Selenium')
deselect_by_index(args) − Deselection with the help of index of option.
This method deselects the option based on the index of the specific option. The index of the elements mostly start with 0. NoSuchElementException thrown if there is no index which matches with the index given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.deselect_by_index(1)
deselect_by_visible_text(args) − Deselection with the help of text displayed of option.
This method is the simplest one which deselects the option based on the visible text. NoSuchElementException thrown if there is no option which matches with the text given in the argument.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.deselect_by_visible_text('Tutorialspoint')
deselect_all() − Deselection of all selected options.
This method is applicable where more than one selection of options can be done. It removes all the options selected from the dropdown. NotImplementedError thrown if it is not possible to select multiple options from the dropdown.
Syntax −
d = Select(driver.find_element_by_id("selection")) d.deselect_all()
all_selected_options() − List of all selected options.
This method is applicable where more than one selection of options can be done. It returns the list of options selected under the select tag.
Syntax −
d = Select(driver.find_element_by_id("selection")) o = d.all_selected_options()
first_selected_option() − Returns the first selected option.
This method returns the currently selected option in a dropdown. It is also applicable to multiple select dropdowns where it will return the first selected option in the dropdown.
Syntax −
d = Select(driver.find_element_by_id("selection")) o = d.first_selected_option()
options() − List of all options. It returns the list of all options under the select tag.
Syntax −
d = Select(driver.find_element_by_id("selection")) o = d.options()
- Related Articles
- What are the various waits available in Selenium with python?
- What are the various methods available under Select class in Selenium?
- What are static methods in a Python class?
- What are assertions available to test relational comparisons in Selenium with python?
- What are the differences between close() and quit() methods in Selenium with python?
- What are the differences between switch_to_default_content() and switch_to.parent_frame() methods in Selenium with python?
- What are the differences between current_window_handle and window_handles methods in Selenium with python?
- What to press ctrl +f on a page in Selenium with python?
- What to press ctrl +c on a page in Selenium with python?
- What are Class/Static methods in Java?
- Save a Web Page with Python Selenium
- What are the best practices for exception handling in Python?
- Describe some of the exceptions available in Selenium with python?
- Methods for Handling Deadlocks
- What are different Navigator methods available?
