Articles → SELENIUM → Handling Drop Downs In Selenium

Handling Drop Downs In Selenium






Scenario




<!DOCTYPE html>
<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<select id="AllOptions">
			<option>Option 1</option>
			<option>Option 2</option>
			<option>Option 3</option>
		</select>
	</body>
</html>


Picture showing the sample dropdown to be handled by selenium
Click to Enlarge




Code




package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MyClass {
	@Test
	public void TestAnnotation() {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Select selectObject = new Select(driver.findElement(By.id("AllOptions")));

		for (int i = 0; i < selectObject.getOptions().size(); i++) {
			System.out.println(selectObject.getOptions().get(i).getText());
		}
	}
}



Output


Picture showing how to handle the dropdown in selenium
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, April 24, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250