To start with WebDriver we need to learn about some of the useful APIs that are provided for automating user actions on an application.
Let us list some of the actions that we need to automate while automating a test case:
- Click a link, button
- Type value in an Edit box
- Select a value from the dropdown
- “Check / Uncheck “ a checkbox
- Select a radio button
This is exactly what I am going to discuss in this post.
Click a link / button:
To click on an object through webdriver first we need to find out which locator we are going to use. Is it ID, name, xpath, or css? For this purpose we can utilize firebug / xpath checker to find out is there any id / name exists for the object we are going to perform action upon. Then write the code as below:
driver.findElement(By.xpath("//a[@href='CustomerInfo.htm']")).click();
In the above line of code “driver” could be FirefoxDriver, InternetExplorerDriver, ChromeDriver, HtmlUnitDriver, etc. On one of these browsers we are going to find an element and then click as per the code.
findElement is an API provided by the webdriver which requires argument “By.xpath”. The “xpath” can be replaced by one of the below methods if we need to identify the element with any other attributes such as css, name, classname, etc:
- className
- cssSelector
- linkText
- name
- partialLinkText
- tagName
- xpath
- id
To understand the above methods one needs the basic understanding of the HTML. Id, name, input, type, a, etc are the HTML tags / attributes. Using these HTML tags and attributes “xpath” can be constructed and this I have already explained in one of my earlier posts (How to write xpath)
Type value in an Editbox
Have a look at the below line of code.
driver.findElement(By.name("FirstName")).sendKeys("Google");
Here the webdriver finds the object first with findElement API and then keys in the value with sendKeys method.
Select a value from the dropdown
To select a value from a dropdown, follow the below steps:
- Declare a List and assign all the values of dropdown using findElements method
- Use a for loop to go through the elements one by one
- Using an IF condition match the required option
- Click the required option (.setSelected is deprecated)
Use the below code and put that into a function which does the job.
public static void selectValue(String valToBeSelected){
List <WebElement> options = driver.findElements(By.tagName("option"));
for (WebElement option : options) {
if (valToBeSelected.equalsIgnoreCase(option.getText())){
option.click();
}
}
}
call the static method wherever necessary – selectValue(“Texas”) will select the value Texas from the dropdown country.
“Check / Uncheck “ a checkbox
To “Check / Uncheck” a checkbox, the object needs to be identified using findElement method and then just click. To find out whether the checkbox is checked or not utilize the method – element.isSelected()
WebElement kancheck = driver.findElement(By.name("kannada"));
kancheck.click();
System.out.println(kancheck.isSelected());
Above code snippet will first click the checkbox named kannada and then verifies whether it is clicked or not.
Select a radio button
Follow the same steps which are used in Checkbox to select a radio button and then verify the status using isSelected() method.
WebElement gender = driver.findElement(By.xpath("//input[@name='male']"));
gender.click();
System.out.println(gender.isSelected());
Above are the basic actions needed for Test Automation. I will try to explain this with an example in the next post.

{ 12 comments… read them below or add one }
Hi Seetaram,
I want to select country as “India” from drop down list, I have written code as given below.
But I’m getting compilation error while doing so. Can you please let me know the reason.
Also, don’t we have direct api to select value from drop down list in Webdriver?
public void selectValue(String valToBeSelected){
WebElement we = driver.findElement(By.name(“country”));
//error in the below line as “the type list is not generic, it can not be parameterised with args”
List options = we.findElements(By.tagName(“option”));
for (WebElement option : options)
{
if (valToBeSelected.equalsIgnoreCase(option.getText()))
{
option.click();
break;
}
}
}
Thanks in adv
Rahul
Dear Rahul,
The reason might be:
1. WebElement is not imported
2. List is not imported
Solution:
import java.util.List;
import org.openqa.selenium.WebElement;
Use the below code:
Select select = new Select(driver.findElement(By.id(“salutation”)));
select.selectByValue(ValToBeSelected);
We can modify your code as below:
Select select = new Select(driver.findElement(By.name(“country”)));
select.selectByValue(“Mr.”);
For the above code to work, you need to add the import statement as below:
import org.openqa.selenium.support.ui.Select;
– Seetaram
Thank you very much Seetaram, I have tried on both the ways given above.. Working fine now..
I have one quick query ::
Suppose this is the source ::
Afghanistan
Albania
India
China
Here,
Select sel = new Select(driver.findElement(By.name(“country”)));
sel.selectByValue(“”);
–> It selected China, As the value given is empty which matches with the value of China
Select sel = new Select(driver.findElement(By.name(“country”)));
sel.selectByIndex(3);
–> It selected China , As index starts from 0 and China is the 3rd element
If I want to select by option’s text i.e by using “India”, rather than using value as 3 or index as 2, How can I do that?
–Rahul
Afghanistan
Albania
India
China
select name=”country”
option value=”1″>Afghanistan /option
option value=”2″>Albania /option
option value=”3″>India /option
option value=”*”>China /option
/select
sel.selectByValue(“India”);
Hi Seetaram,
In
I want to select “Unread” value from drop down in gmail home screen.
In testCreateAccount Class, i write below code:
selectValue(driver,”Unread”);
and calling by
public static void selectValue(WebDriver driver, String valToBeSelected){
List options = driver.findElements(By.id(“:pj”));
for (WebElement id : options) {
if (valToBeSelected.equalsIgnoreCase(id.getText())){
id.click();
}
}
But its not selecting “Unread” from drop down. Please suggest.
Above method does not work for this… check the HTML source code associated with Gmail home and then you need to tweak this java method according to your needs…
hi ,seetaram.,
Can u give some examples of webdriver apis like find element
thanks and regards.,
Babu.m
Hi Seetaram,
I’m trying to select a checkbox but webdriver is not able to find the element on page, it is selecting radio boxes just fine;
WebElement element = driver.findElement(By.id(“123″));
if(element.isDisplayed()) {
element.click();
System.out.println(element.isSelected());
}
But it gives error that Unable to locate element. Any idea?
Thanks
Please give example / usage of isSelected() for a dropdownlist. My requirement is to verify selected item from the dropdown list.