Hello Tushar, if by handling Dropdown you mean selecting an option/options from the dropdown menu, then you can do this in following ways:
- select_by_index(desired_index_value)
- select_by_visible_text(“text_to_be_selected_from_drop_down_menu”)
- select_by_value(value)
In the following example, different methods are shown through which you can select an element from the menu (@aria-label=’select’):
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from pip._vendor.distlib import resources
driver = webdriver.Firefox()
driver.get("http://demos.dojotoolkit.org/dijit/tests/test_Menu.html")
sleep(5)
try:
dropdown_element = Select(driver.find_element_by_xpath("//select[@aria-label='select']"))
# Option 1 - Selecting the drop-down item by using the text
dropdown_element.select_by_visible_text("bleed through")
sleep(5)
# Option 2 - Selecting the drop-down item by using the index value
dropdown_element.select_by_index(0)
sleep(5)
# Option 3 - Selection of desired option using value
# select_element.select_by_value('2')
except NoSuchElementException:
print("Element not found")
sleep(5)
driver.quit()