This program opens a webpage with a dropdown menu for states. It selects options by value, visible text, and index, printing confirmation each time.
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 DropdownExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/dropdown");
WebElement dropdown = driver.findElement(By.id("state"));
Select select = new Select(dropdown);
select.selectByValue("CA");
System.out.println("Selected by value: CA");
select.selectByVisibleText("New York");
System.out.println("Selected by visible text: New York");
select.selectByIndex(3);
System.out.println("Selected by index: 3");
driver.quit();
}
}