You are on page 1of 6

SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

WebDriver Code using Selenium Select Class


Step 1: Create a new java class named as HandlingDropDown under the
Step 2: Copy and paste the below code in the HandlingDropDown.java class.
Below is the test script
WebDriver driver=new FirefoxDriver();
//Launch the application
driver.navigate().to("http://newtours.demoaut.com/");
//Type user name by identifying user name text box
driver.findElement(By.name("userName")).sendKeys("mercury");
//Identify the password text filed and type password
driver.findElement(By.name("password")).sendKeys("mercury");
//Identify the sign in button and click on that button
driver.findElement(By.xpath("//input[@value='Login']")).click();
//Identify drop down
Select ByValue=new Select(driver.findElement(By.name("fromPort")));
//select dropdown option by using visible text
ByValue.selectByVisibleText("Sydney");
//select dropdown option by using value
ByValue.selectByValue("London");
//select dropdown option by using index
ByValue.selectByIndex(2);
//Get drop down options
List<WebElement> options=ByValue.getOptions();
for(int i=0;i<options.size();i++)
{
String optionvalue=options.get(i).getText();
System.out.print("Drop Down Values:"+optionvalue+"\n");

Page 1 of 6

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

Import Statements
-----------

import org.openqa.selenium.support.ui.Select Import this package prior to the script


creation. The package references to the Select class which is required to handle the
dropdown.

Object Instantiation for Select class


Select selectByValue = new Select(driver.findElement(By.id(SelectID_One)));
We create a reference variable for Select class and instantiate it using Select class and the
identifier for the drop down.
The identifier or the locator value for the drop down can be found using the techniques discussed
in the initial tutorials (by using Selenium IDE and firebug).
Take a notice that the identifier for a dropdown can be found as below:
Step 1: Most or almost all the dropdowns elements are defined in the <Select> tag having
multiple values (values that can be set into the dropdown) that are defined under the <option>
tags.

Setting the value in the dropdown using selectByValue() method

Page 2 of 6

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

selectByValue.selectByValue(greenvalue);
In the above java command, we select the value green in the drop down using the
selectByValue() method and parameterizing it with the text present in the value attribute.

Setting the value in the dropdown using selectByVisibleText() method


selectByValue.selectByVisibleText(Lime);
In the above java command, we select the value Lime in the drop down using the
selectByVisibleText() method and parameterizing it with the text present on the user interface or
the text present between the opening and closing <option> tags.

Setting the value in the dropdown using selectByIndex() method


selectByValue.selectByIndex(2);
Page 3 of 6

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

In the above java command, we select the third value in the drop down using the selectByIndex()
method and parameterizing it with the index value of the element which is desired to be selected
in the dropdown.
Take a note that the index value starts with 0.
Moving ahead in the Selenium series, we would be discussing about the various types of looping
and conditional commands in WebDriver like isSelected(), isEnabled() and isDispalyed(). These
methods are used to determine the visibility scope for the web elements.
So let us start with a brief introduction WebDriver has a W3C specification that details out the
information about the different visibility preferences based out on the types of the web elements
upon which the actions are to be performed.
WebDriver facilitates the user with the following methods to check the visibility of the web
elements. These web elements can be buttons, dropboxes, checkboxes, radio buttons, labels etc.

isDisplayed()

isSelected()

isEnabled()

For an improved understanding, let us discuss the aforementioned methods with code examples

WebDriver Code
Step 1: Create a new java class named as VisibilityConditions project.
Step 2: Copy and paste the below code in the VisibilityConditions.java class.
Following are the ways in which we ascertain the presence of web elements on the web page.
boolean submitbuttonPresence=driver.findElement(By.id(gbqfba)).isDisplayed();

isDispalyed()
isDisplayed() is the method used to verify presence of a web element within the webpage. The
method is designed to result a Boolean value with each success and failure. The method returns a
true value if the specified web element is present on the web page and a false value if the
web element is not present on the web page.

Page 4 of 6

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

Thus the above code snippet verifies for the presence of submit button on the google web page
and returns a true value if the submit button is present and visible else returns a false value if the
submit button is not present on the web page.
boolean searchIconEnabled = driver.findElement(By.id(gbqfb)).isEnabled();
The method deals with the visibility of all kinds of web elements not just limiting to any one
type.

isEnabled()
isEnabled() is the method used to verify if the web element is enabled or disabled within the
webpage. Like isDisplayed() method, it is designed to result a Boolean value with each success
and failure. The method returns a true value if the specified web element is enabled on the web
page and a false value if the web element is not enabled (state of being disabled) on the web
page.
Thus the above code snippet verifies if the submit button is enabled or not and returns a Boolean
value depending on the result.
The isEnabled() method is significant in scenarios where we want to ascertain that only if
Condition A is fulfilled, then the element(principally button) is enabled. Refer the following
illustration for the same.

In the above figure, Register button button is enabled only when the agreement checkbox is
selected.
Akin to above methods, we have a method referenced as isSelected() which tests if the
specified web element is selected or not.
boolean searchIconSelected = driver.findElement(By.id(male)).isSelected();
Page 5 of 6

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

isSelected()
isSelected() is the method used to verify if the web element is selected or not. isSelected()
method is pre-dominantly used with radio buttons, dropdowns and checkboxes. Analogous to
above methods, it is designed to result a Boolean value with each success and failure.
Thus the above code snippet verifies if the male radio button is selected or not and returns a
Boolean value depending on the result. Refer the following image for the same.

Page 6 of 6

You might also like