In what ways we can handle dropdown menu in a page using Selenium with Python?

0 votes
In what ways we can handle dropdown menu in a page using Selenium with Python?
Jul 23 in Selenium by Tushar
41 views

1 answer to this question.

0 votes

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()
answered Jul 23 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
1 answer
+2 votes
1 answer

How can we handle authentication popup in Selenium WebDriver using Java

The Alert Method, authenticateUsing() lets us skip ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,650 points
8,586 views
0 votes
1 answer
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 13,960 points
231 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
956 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,124 views
0 votes
1 answer