WebDriver Tutorial 3 : Mastering WebDriver API

This is Part 3 of the Selenium WebDriver Tutorial Series. Check out Part 2 to know about locating elements and mastering XPath.

Master WebDriver API

For any automation tester interested in Selenium WebDriver, mastering the concepts of WebDriver API is of prime importance. In this post, we will be going deep into the WebDriver API; mainly focusing on Navigation, working with Web Elements and WebDriver Wait in this tutorial.

Navigation using WebDriver API

Get Method

The normal way of fetching a page is using the get method. WebDriver will wait till the page is fully loaded (ie the onload event has fired) before going onto the next statement.

But, there is another way of navigation using the navigate() method.

Navigate Methods

  • The driver.navigate().to() statement does the same thing as driver.get(), just that one is easier to type than the other.
  • driver.navigate().back(); loads the previously loaded url
  • driver.navigate().forward(); loads the next url (if you have used navigate.back());
  • driver.navigate().refresh(); refreshes the current page

Working with Web Elements

Form Elements

Text Fields and Text Area

We can type on text fields and text areas using the sendKeys method using the below syntax

Check out this post to know more about locator and xpaths.

The Keys class comes in handy while dealing with text fields/areas. Using Keys, we can simulate various key presses. For eg: To press ENTER key after typing a string, we use the following code.

SEE ALSO:   How to Run TestNG xml Programmatically

Check the documentation page to see the list of available Keys options. You can also type ‘Keys.’ in Eclipse and press Ctrl + Space to see the suggestions.

To clear the contents of a text field or text area, use the clear() method as shown below.


We can get the current state of checkboxes using the isSelected method of WebElement.

For selecting or unselecting a checkbox, get the current state of the checkbox and then use click method to select/unselect it.

List Boxes

The Select object is used to deal with list boxes (select tag). We use findElement to get the Select web element and use the Select objects properties to operate on it.

Form Submission

Forms can be submitted by using the submit method on either the submit button on any of the form elements.

SEE ALSO:   WebDriver Tutorial 1 : Basic Project Setup

 WebDriver Wait

There can be occasions where we may have to wait till the elements are fully loaded so that we can perform operations on them. There are two types of waits in WebDriver, though I never use the first.

Implicit Wait

Implicit wait makes webdriver wait for a certain amount of time if the element in question is not immediately available. Once set, the implicit wait is set for the life of the WebDriver object instance. The below statement will make webdriver wait for 10 sec.

This method is not preferred as it will unnecessarily waste time.

Explicit Wait

Explicit wait waits for a certain condition to satisfy before moving onto the next statement. We can ask webdriver to wait till an element comes to a particular state (becomes visible, becomes clickable etc). Below is the useage

Here, it waits till the ExpectedCondition of elementToBeClickable is satisfied – ie Waits till the given element becomes available and clickable. Other expected conditions are available such as elementToBeSelected, textToBePresentInElement, visibilityOfElementLocated etc. So its always a better option to use wait till the element is in the expected state in case of highly dynamic web pages.

I hope I have covered the important points on WebDriver API. Please leave a comment below in case I missed anything.

We will be seeing Window Handling in the next post.

Don’t forget to check out our Cheat Sheet for Selenium WebDriver

You may also like...

3 Responses

  1. Rekha says:

    Hi Amal, this is a good article covering all the necessary information. I have a question w.r.t. Explicit Waits. Say , we are using Page Factory and Page Object model, where the element locators in the Page Class for every page. The Explicit wait takes the By Object as a parameter for most ExpectedConditions like ElementIsVisible, ElementExists and some others too. With Page Factory, the locators are defined as below….

    [FindsBy(How=How.Id, Using = “Amount”)]
    private IWebElement _amount{ get; set; }

    How do you use Explicit waits in such cases ?

    • Amal Bose says:

      You can have your PageObject class Extend LoadableComponent class which makes you override isLoaded() and load() methods.
      You can have the code to verify the page load inside isLoaded() (other elements which are loaded when page is loaded). And before use of the dynamically loaded element, you can use WebDriverWait to wait till its completely loaded.

      Check this StackOverflow answer http://stackoverflow.com/a/18844275/577202

  1. April 26, 2016

    […] Tutorial 3 : Mastering WebDriver API […]

Leave a Reply

Your email address will not be published. Required fields are marked *