How can I approach towards to select a drop-down option by using the selenium?

395    Asked by Aashishchaursiya in QA Testing , Asked on Jun 5, 2024

 I am currently engaged in a particular task that is related to automating a website registration process by using Selenium I Python. One of the steps involves the selection of a country from a drop-down menu. How can I approach this task by using the Selenium method and Python programming capabilities? 

Answered by Damini das

 In the context of selenium, here are the steps given below:-

Locate the drop-down element

You can use the selenium for finding the drop-down element by using I ID, name, Xpath, or CSS selector.

Select options by visible text

If the drop-down options are visible text then you can select an option like below.

Select options by index

Alternatively, you can also select an option by its index.

Select an option by value

If the drop-down options have specific values associated with them then you can select by its value.

Handling drop-down with javascript

In some cases, if the above-mentioned strategy doesn’t work reliably then you can use JavaScript for interacting with the drop-down.

Here is the example given below in Python programming language:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Import org.openqa.selenium.support.ui.Select;
Public class DropdownSelection {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the website with the dropdown
        Driver.get(https://example.com);
        // Locate the dropdown element by ID
        WebElement dropdown = driver.findElement(By.id(“countryDropdown”));
        // Create a Select object for the dropdown
        Select select = new Select(dropdown);
        // Select option by visible text
        Select.selectByVisibleText(“United States”);
        // Alternatively, select by index
        // select.selectByIndex(1); // selects the second option
        // Alternatively, select by value
        // select.selectByValue(“US”); // selects the option with value “US”
        // Close the browser
        Driver.quit();
    }
}

# Importing necessary modules from Selenium

From selenium import webdriver

From selenium.webdriver.support.ui import Select
From selenium.webdriver.common.by import By
From selenium.webdriver.support.ui import WebDriverWait

From selenium.webdriver.support import expected_conditions as EC

# Setting up the Chrome webdriver
Driver = webdriver.Chrome()
# Navigating to the website with the dropdown
Driver.get(https://example.com)
# Explicit wait for the dropdown element to be present
Dropdown_locator = (By.ID, “countryDropdown”)
Wait = WebDriverWait(driver, 10)
Dropdown = wait.until(EC.presence_of_element_located(dropdown_locator))
# Creating a Select object for the dropdown
Select = Select(dropdown)
# Selecting an option by visible text
Desired_option = “United States”
Select.select_by_visible_text(desired_option)
# Alternatively, selecting an option by index
# select.select_by_index(1) # selects the second option
# Alternatively, selecting an option by value
# select.select_by_value(“US”) # selects the option with value “US”
# Closing the browser window
Driver.quit()
Here is the example given in java programming language:-
From selenium import webdriver
From selenium.webdriver.support.ui import Select
# Initialize the Chrome driver
Driver = webdriver.Chrome()
# Open the website with the dropdown
Driver.get(https://example.com)
# Locate the dropdown element by ID
Dropdown = driver.find_element_by_id(“countryDropdown”)
# Create a Select object for the dropdown
Select = Select(dropdown)
# Select option by visible text
Select.select_by_visible_text(“United States”)
# Alternatively, select by index
# select.select_by_index(1) # selects the second option
# Alternatively, select by value
# select.select_by_value(“US”) # selects the option with value “US”
# Close the browser window
Driver.quit()

Here is the example given in HTML:-




    <meta</span> charset=”UTF-8”>

    <meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

    Dropdown Test Page



    Registration Form

   


        Select your country:

       

       


       

   






Your Answer

Interviews

Parent Categories