WebDriver has a class called “Select” to interact with dropdown. Dropdown
is an another WebElement like textbox, radio button, links etc.
We use below methods frequently to interact with Dropdown using Select
class.
selectByIndex(int index)
selectByValue(String value)
selectByVisibleText(String text)
getOptions()
getFirstSelectedOption()
Note: Select class can be
found in org.openqa.selenium.support.ui package
Sample Dropdown:
selectByIndex(int index):
This method is used to select dropdown value by using index. This
method accept integer as an argument. Index value always start from 0 (Zero).
So if we want to select 2nd value from dropdown we have to give
index 1.
Syntax:
Select s = new Select(dropdown_WebElement);
s.selectByIndex(1);
Example:
package selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class HandleDropdown {
public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select second dropdown value using index
s.selectByIndex(1);
}
}
selectByValue(String value):
This method is used to select dropdown value by using value. This
method accepts String as an argument. All dropdowns may not have ‘value’
attribute in html code. If we have ‘value’ attribute for dropdown option, then
we can use this method.
Syntax:
Select s = new Select(dropdown_WebElement);
s.selectByValue(“value”);
In above dropdown we have ‘value’ attribute. So here we can use selectByValue()
method.
Example:
package selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class HandleDropdown {
public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select dropdown value using 'Value'
s.selectByValue("1");
}
}
selectByVisibleText(String text):
This method is used to select dropdown value by using visible text. This
method accepts String as an argument. Visible text means the text which is
visible in dropdown.
Select all options that display text matching the argument. That is,
when given "Bar" this would select an option like: <option
value="foo">Bar</option>
Note: This is case sensitive.
Syntax:
Select s = new Select(dropdown_WebElement);
s. selectByVisibleText (“visible text”);
Example:
package selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class HandleDropdown {
public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select dropdown value using 'visible text'
s.selectByVisibleText("Who was your Childhood hero?");
}
}
getOptions():
This method is used to get all options from dropdown. It return List of
WebElement ( List<WebElement>) then we can iterate using for loop or
iterator and using getText() method we can extract values.
Syntax:
Select s = new Select(dropdown_WebElement);
s. getOptions();
Example:
package selenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class HandleDropdown {
public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// get all dropdown options using getOptions method
List<WebElement> allOptions = s.getOptions();
for (int i = 0; i < allOptions.size(); i++) {
System.out.println("Dropdown Option "+(i+1)+" is: "+allOptions.get(i).getText());
}
}
}
Output:
Dropdown Option 1 is: ----Select your Security Question----
Dropdown Option 2 is: What is your pet name?
Dropdown Option 3 is: What was the name of your first school?
Dropdown Option 4 is: Who was your Childhood hero?
Dropdown Option 5 is: What is your favorite past-time?
Dropdown Option 6 is: What is your all time favorite sports team?
Dropdown Option 7 is: What is your fathers middle name?
Dropdown Option 8 is: What make was your first car or bike?
Dropdown Option 9 is: Where did you first meet your spouse?
getFirstSelectedOption():
It will return the first selected option in dropdown. Using getText()
method we can retrieve the value.
In some dropdowns as per client requirement some default value will be
display. To check that default value is displaying or not we can use this
method.
Syntax:
Select s = new Select(dropdown_WebElement);
s. getFirstSelectedOption ();
Example:
package selenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class HandleDropdown {
public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// get default selected option
WebElement firstOption = s.getFirstSelectedOption();
System.out.println("First selected option is: "+firstOption.getText());
}
}
Output:
First selected option is: ----Select your Security Question----
Comments
Post a Comment