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
1,634 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 24, 2019 by Abha
• 28,140 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,150 points
11,136 views
+2 votes
3 answers

How can we handle authentication popup in Selenium WebDriver using Java

1) By passing user credentials in URL. String ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
28,347 views
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 14, 2019 in Selenium by Pratibha
• 3,690 points
2,861 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,150 points
15,138 views
0 votes
2 answers

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,500 points
12,615 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,571 views
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,910 points
6,629 views
+1 vote
1 answer

Can I select multiple options from a dropdown using Python Selenium webdriver?

Hi Dhwani, you can use following code ...READ MORE

answered Jul 31, 2019 in Selenium by Abha
• 28,140 points
4,472 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP