Webdriver Code Using Selenium Select Class
Webdriver Code Using Selenium Select Class
5/2/17
SELENIUM WEBDRIVER
Step 2: Copy and paste the below code in the “HandlingDropDown.java” class.
Page 1 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER
Import Statements
------------
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.
Page 2 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
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.
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.
selectByValue.selectByIndex(“2”);
Page 3 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
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.
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.
Handling pop up is one of the most challenging piece of work to automate while testing
web applications. Owing to the diversity in types of pop ups complexes the situation
even more.
It is nothing but a small box that appears on the display screen to give you some kind of
information or to warn you about a potentially damaging operation or it may even ask
you for the permissions for the operation.
Example: Let us consider a real life example for a better understanding; Let us assume
that we uploaded a photograph on any of these popular social networking sites. Later
on, i wish to delete the uploaded photograph. So in order to delete, i clicked on the
delete button. As soon as I click on the delete button, the system warns me against my
action, prompting – Do you really want to delete the file? So now we have an option to
either accept this alert or reject it.
So ahead in the session, let’s see how do we reject or accept the alerts
depending on their types. Starting with the web based pop ups.
Page 4 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER
WebDriver offers the users with a very efficient way to handle these pop ups using Alert
interface.
There are the four methods that we would be using along with the Alert interface.
1) void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the
pop up window appears.
2) void accept() – The accept() method clicks on the “Ok” button as soon as the pop up
window appears.
3) String getText() – The getText() method returns the text displayed on the alert box.
4) void sendKeys(String stringToSend) – The sendKeys() method enters the specified
string pattern into the alert box.
Copy the below code in note pad and save it as .Html file, click on the Try It button alert
will generate
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction()
Page 5 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER
confirm("Press a button!");
</script>
</body>
</html>
driver.navigate().to("file:///C:/Users/s.r.ankireddygari/Desktop/webdriver(selenium)/
Alerts.HTML");
//switch to alert
Alert alert=driver.switchTo().alert();
//click on ok
alert.accept();
driver.findElement(By.xpath("//button")).click();
alert=driver.switchTo().alert();
//click on cancel
alert.dismiss();
driver.findElement(By.xpath("//button")).click();
alert=driver.switchTo().alert();
Page 6 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER
Page 7 of 7