logo

The best IT Trainig Institute In Gurgaon

Handle Dropdown In Selenium

write your Package Name (asc) & Class Name (IIT) otherwise error will show.

package asc;

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.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.locators.RelativeLocator.with;

import java.util.List;
import java.util.Set;

import io.github.bonigarcia.wdm.WebDriverManager;

public class IIT {

	public static String browser = "Chrome";
	public static WebDriver driver;

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		if (browser.equals("Firefox")) {
			WebDriverManager.firefoxdriver().setup();
			driver = new FirefoxDriver();

		} else if (browser.equals("Chrome")) {
			WebDriverManager.chromedriver().setup();
			driver = new ChromeDriver();

		}

		else if (browser.equals("Safari")) {
			WebDriverManager.safaridriver().setup();
			driver = new SafariDriver();

		} else if (browser.equals("Edge")) {
			WebDriverManager.edgedriver().setup();
			driver = new EdgeDriver();

		}

		driver.get("https://www.sugarcrm.com/au/request-demo/");

	    driver.manage().window().maximize();
	    WebElement ddown=driver.findElement(By.name("employees_c"));
	    Select select =new Select(ddown);
	    
	    select.selectByValue("level1");
	    Thread.sleep(2000);
	    
	    select.selectByVisibleText("51 - 100 employees");
	    Thread.sleep(2000);
    		
	    select.selectByIndex(6);
	    Thread.sleep(2000);
    		
		
    }
}
                

Output
output

Script Overview

  • Browser Setup:
    The script checks which browser to use based on the browser variable ("Chrome" in this case) and sets up the appropriate WebDriver using WebDriverManager.
  • Navigating to a Web Page:
    The script navigates to the URL
    "https://www.sugarcrm.com/au/request-demo/".
  • Maximizing the Window:
    The browser window is maximized to ensure all elements are visible and accessible.

Handling Dropdowns:

  • The script identifies a dropdown element on the page using
    driver.findElement(By.name("employees_c"))
    The name attribute of the dropdown element is "employees_c".
  • A Select object is created to interact with the dropdown. This object provides various methods to select options from the dropdown.

Handling Dropdowns with the Select Class

The Select class in Selenium provides methods to select options from a dropdown list. Here’s how these methods are used in the script:

Select by Value:

  • select.selectByValue("level1"); 
  • This selects an option with the value attribute "level1" in the dropdown.

Select by Visible Text:

  • select.selectByVisibleText("51 - 100 employees");
  • This selects an option that is displayed as "51 - 100 employees".

Select by Index:

  • select.selectByIndex(6);
  • This selects the option at the 6th index (7th position, as index starts from 0).

This script provides a basic framework for selecting options from a dropdown and interacting with elements on a webpage using Selenium WebDriver.