Select Dropdown values using Selenium WebDriver

Select Dropdown values

Selenium WebDriver provides n number of inbuilt methods to perform actions on appliaction under test. For selecting dropdown values, selenium provides below three methods:

  • 1. selectByIndex
  • 2. selectByValue
  • 3. selectByVisibleText

selectByIndex means select dropdown value using its index. Index starts from zero.
for selectByValue and selectByVisibleText , inspect using firebug to see its value and visible text.(Value and visible text can be same or different)
Refer below image and selenium code for the same

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package scripts;

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 selectDropdownValues {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

//set chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//get the Locator of drop down box
WebElement dropdown = driver.findElement(By.name("ustate"));

//Create object of Select Class
Select select = new Select(dropdown);

//We can select Drop down value by three ways:

//Method 1: Select drop down value using index
select.selectByIndex(4);

Thread.sleep(2000);

// Method 2: Select drop down value using index
select.selectByValue("Chhattisgarh");

Thread.sleep(2000);

//Method 3: Select drop down value using index
select.selectByVisibleText("Maharashtra");


}

}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×