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, 2019 in Selenium by Tushar
97 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, 2019 by Abha
• 27,760 points

Related Questions In Selenium

0 votes
1 answer

Can we perform zoom-in or zoom-out actions in a webpage using Python Selenium?

Hello Ruhi, to perform Zoom-in or Zoom-out ...READ MORE

answered Jul 29, 2019 in Selenium by Anvi
• 14,010 points
729 views
+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,710 points
11,796 views
0 votes
1 answer
0 votes
1 answer

How can I scroll a web page in Mozilla Firefox using Selenium?

Hi Rohan, steps to scroll a webpage in ...READ MORE

answered May 13, 2019 in Selenium by Pratibha
• 3,690 points
443 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8, 2019 in Selenium by Anvi
• 14,010 points
463 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,510 points
1,786 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,870 points
1,611 views
0 votes
1 answer