Menu Close

Selenium WebDriver Dropdown Menu Commands and Conditional Commands in selenium python- Devduniya

selenium conditional commands
Rate this post

In this blog, we are going to learn selenium dropdown menu commands and conditional commands with python programming. We have published articles related to the basic concepts of selenium, you can read those articles below or on our website. So let’s start this article without wasting any time”-

In Selenium with Python, a dropdown menu is a list of options that can be selected by the user. Dropdown menus are often used on websites to allow users to select from a list of options, such as choosing a country or a language.
To interact with a dropdown menu in Selenium, you can use the Select class. To use this class, you first need to locate the dropdown element using one of the find_element_by_* methods, such as find_element_by_id or find_element_by_xpath. You can then create a Select object by passing the dropdown element as an
argument.

3 ways to select dropdown:-

  • select_by_visible text
  • select_by_value
  • select_by_index

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get devduniya.com
driver.get('https://devduniya.com/')
# Maximizing Window
driver.maximize_window()
# Locate the dropdown element
dropdown_element = driver.find_element(By.ID, "dropdown")
# Create a Select object
select = Select(dropdown_element)

Once you have a Select object, you can use its methods to interact with the dropdown menu. Some common methods include:
select_by_index(index): Selects the option at the given index (starting at 0)
select_by_value(value): Selects the option with the given value attribute
select_by_visible_text(text): Select the option with the given visible text

For example:

# Select the second option
select.select_by_index(1)
# Select the option with value="option2"
select.select_by_value("option2")
# Select the option with visible text "Option 3"
select.select_by_visible_text("Option 3")

Conditional commands in Selenium are used to execute a certain action or set of actions only if a certain condition is met. For example, you might want to click a button only if it is enabled or fill out a form only if a certain dropdown menu has a specific value selected.
To use conditional commands in Selenium, you can use an if statement and check the desired condition using one of the find_element_by_* methods and the appropriate property of the element.
There are three types of conditional commands that I have used are:

  • is_displayed()
  • is_enabled()
  • is_selected()

Now we will cover all these conditional commands one by one with examples.
is_disaplyed(): This function is used to check if any tag is displaying or not and it is used to give output in the format of True or False.
is_enabled(): This function is also used to check if any tab is enabled or not and it also gives output in the format of True or False.
is_selected(): This function is used to check if any button is selected or not like we can use this function to check if a radio button is selected or not. And it will
return output in the format of True or False.

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get devduniya.com
driver.get('https://devduniya.com/')
# Maximizing Window
driver.maximize_window()
# Locate the button element
button = driver.find_element(By.ID, "dropdown")
# Check if the button is enabled
if button.is_enabled():
  # Click the button
  button.click()
else:
    pass

You can also use the try and except statements to handle errors that might occur if the element is not found or the desired property is not present.

For example:

from selenium.common.exceptions import NoSuchElementException
# Try to locate the dropdown element
try:
  dropdown = driver.find_element(By.ID, "dropdown")
except NoSuchElementException:
  print("Dropdown element not found")
  dropdown = None
# If the dropdown element was found, check if the value is "option2"
if dropdown and dropdown.get_attribute("value") == "option2":
  # Do something

So guys I hope you all understood about dropdown menu commands and conditional commands used in selenium. We have successfully covered all the dropdown menu commands and conditional commands in selenium python programming. We have discussed the functions of conditional commands and dropdown menu commands.
In the next tutorial, we have covered some advanced concepts of Selenium Webdriver using python programming.

Suggested Blog Posts

Leave a Reply

Your email address will not be published.