<html>
<head>
<title>Select Example by Index value</title>
</head>
<body>
<select name="Mobiles"><option value="0" selected> Please select</option>
<option value="1">iPhone</option>
<option value="2">Nokia</option>
<option value="3">Samsung</option>
<option value="4">HTC</option>
<option value="5">BlackBerry</option>
</select>
</body>
</html>
--------------------------
select.selectByValue(Value);
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByIndex(1);
------------------------------------
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByVisibleText("HTC");
-------------------------
<html>
<head>
<title>Select Example by Value</title>
</head>
<body>
<p>Which mobile device do you like most?</p>
<select name="Mobiles"><option selectd> Please select</option>
<option value="iphone">iPhone</option>
<option value="nokia">Nokia</option>
<option value="samsung">Samsung</option>
<option value="htc">HTC</option>
<option value="blackberry">BlackBerry</option>
</select>
</body>
</html>
Webdriver code for Selecting a Value using select.selectByValue(Value);
public class selectExamples {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByValue("nokia");
}
}
deselect methods.
WebElement element=driver.findElement(By.name("Mobdevices"));
Select se=new Select(element);
//Here we will take multi select dropdown to show you the difference
se.selectByVisibleText("HTC");
se.selectByValue("nokia");
//From the above two commands, in the dropdown two values will be selected.
//Now we will try to deselect any of the One
//Im using thread to see the difference when selecting and selecting
Thread.sleep(3000);
se.deselectByValue("nokia");
//You can deselect the value by specifying the index, value and VisibleText
//It will work if you the index is already selected
se.deselectByIndex(1);
//It will deselect if the visible text HTC is in selected mode
se.deselectByVisibleText("HTC");
//It will de-select all the values which are selected
se.deselectAll();