Handling drop-down:
The Select class in selenium is used to handle the drop-down. The drop-down is created in an HTML document using the <select> tag.
The selenium Select class contains the following sub-functions:
- options: This function gets the all options in the dropdown in a list of web elements.
- Syntax:
- dropdown = Select(driver.find_element(By.ID, "tcg"))
- all_options = dropdown.options
- print(len(all_options))
- for option in all_options:
- option.click()
- all_options[2].click()
- select_by_visible_text("VisibleText"): This function selects the options based on the visible text in the drop-down.
- Syntax:
- dropdown = Select(driver.find_element(By.ID, "tcg"))
- dropdown.select_by_visible_text("India")
- select_by_value("value"): This function selects the option based on the value attribute.
- Syntax:
- dropdown = Select(driver.find_element(By.ID, "tcg"))
- dropdown.select_by_value("ind")
- select_by_index(index): To select an option, use the index number. Keep in mind that the index starts from zero, so to select the first option, use 0 as the index. For the second option, use 1 as the index, and so on. By using the correct index, you can easily select the desired option.
- Syntax:
- dropdown = Select(driver.find_element(By.ID, "tcg"))
- dropdown.select_by_index(4)
Similarly, there are functions to deselect the selected option:
- deselect_all()
- deselect_by_visible_text("visibletext")
- deselect_by_value("value")
- deselect_by_index(index)
is_multiple: we can use this function to ensure whether the specified dropdown is a multi-select option dropdown or not. This method returns True when the corresponding dropdown is multi-select.
- Syntax:
- dropdown = Select(driver.find_element(By.ID, "tcg"))
- multidrp = dropdown.is_multiple
- if multidrp:
- print("The Dropdown is Multi-Select")
- else:
- print("The Dropdown is not Multi-Select")