Selenium Tutorials

If you are a beginner, please Proceed here to start from the beginning.

Webdriver SELECT Methods to work with Dropdowns

WebDriver’s support classes called “Select”, which provides useful methods for interacting with select options. User can perform operations on a select dropdown and also de-select operation using the below methods.

Method Name: selectByIndex 

Syntax: select.selectByIndex(Index);
Purpose:  To Select the option based on the index given by the user.
There is an attribute called "values" which will have the index values.

The below is the sample html code using index

Taking Screenshot using Webdriver

Its very important to take screenshot when we execute a test script. When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.

It helps us to debug and identify the problem by seeing the screen shot.

In selenium webdriver, we can take the screen shot using the below command.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

WebDriver Methods

We will look in detail for each of the webdriver methods that we have. The first that we use is 'driver.get(url)' after starting the browser.

When we get the driver object, the below are the methods that we can perform operation on a driver. In IDE like eclipse, when we add period (. ) you will show all the below methods.

How to delete Cookies in Selenium Webdriver

Delete Cookie
Delete Cookie with Name
Delete All Cookies

User can delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.

User can delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.

User can also delete all the cookies for the current domain using driver.manage().deleteAllCookies();


Deleting the specific cookie with cookie name "--utmb"

How to addcookie with Selenium Webdriver

Using webdriver we can easily pass the cookie to the domain. In order to pass cookie, we should use a method named "addCookie(cookie)"

Method Name: addCookie(Cookie cookie)
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name and value of the cookie to be add.


Navigation Methods in Webdriver with Examples


Method Name:
Syntax: driver.navigate().to(URL);
Purpose: This methods Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.
Parameters: URL – It should be a fully qualified URL.


Difference between Webdriver get() and Webdriver navigate()

The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

Syntax: get(java.lang.String url) url - The URL to load. It is always best to use a fully qualified URL

Example: driver.get(""); which loads a new web page in the current browser window.

StaleElementReference Exceptions in Selenium Webdriver

This Exception occurs when driver is trying to perform action on the element which is no longer exists or not valid.

WebElement ele = driver.findElement("sample"));
// Before clicking some thing happened and DOM has changed due to page refresh, or element is removed and re-added;

Now at this point, the element which you're clicking is no longer valid.

so it throws up its hands and gives control to user, who as the test/app author should know exactly what may or may not happen.