Various Commonly and Routinely Used Selenium WebDriver Commands – Selenium Tutorial #17

In the last tutorial, we discussed about the different types of alerts encountered while testing web-based applications and their effective ways of handling. We discussed both the types of alerts i.e. “Web-based alerts” and “Window-based alerts” at length. We also made you acquainted with yet another Java-based utility named as “Robot Class” to handle Windows-based pop-up.

Advancing ahead in this Selenium WebDriver tutorial series, we would be pressing on various commonly and routinely used Selenium WebDriver commands. We will precisely and briefly discuss each of these Selenium commands so as to make you capable of using these commands effectively whenever the situation arises. Selenium WebDriver Commands:

Just to have a rough idea, we would be discussing the following Selenium WebDriver commands and their different versions:

  1. get() methods
  2. Locating links by linkText() and partialLinkText()
  3. Selecting multiple items in a drop dropdown
  4. Submitting a form
  5. Handling iframes
  6. close() and quit() methods
  7. Exception Handling

What You Will Learn:

#1) get() Methods

WebDriver commandUsage
get()• The command launches a new browser and opens

the specified URL in the browser instance

• The command takes a single string type parameter that is usually a URL of application under test

• To the Selenium IDE users, the command may look very much like open command


getClass()The command is used to retrieve the Class object

that represents the runtime class of this object


getCurrentUrl()• The command is used to retrieve the URL of the webpage the user is currently accessing

• The command doesn’t require any parameter and returns a string value


getPageSource()• The command is used to retrieve the page source

of the webpage the user is currently accessing

• The command doesn’t require any parameter and returns a string value

• The command can be used with various string operations like contains() to ascertain the

presence of the specified string value

boolean result = driver.getPageSource().contains("String to find");

getTitle()• The command is used to retrieve the title of the webpage the user is currently working on.

A null string is returned if the webpage has no title

• The command doesn’t require any parameter and returns a trimmed string value

String title = driver.getTitle();

getText()• The command is used to retrieve the inner text

of the specified web element

• The command doesn’t require any parameter and returns a string value

• It is also one of the extensively used commands for verification of messages, labels, errors etc displayed

on the web pages.

String Text = driver.findElement("Text")).getText();

getAttribute()• The command is used to retrieve the value of the specified attribute

• The command requires a single string parameter that refers to an attribute whose value we aspire to know and returns a string value as a result.



getWindowHandle()• The command is used to tackle with the situation when we have more than one window to deal with.

• The command helps us switch to the newly opened window and performs actions on the new window.

The user can also switch back to the previous window if he/she desires.

private String winHandleBefore;

winHandleBefore = driver.getWindowHandle();


getWindowHandles()• The command is similar to that of “getWindowHandle()” with the subtle difference that it helps to deal with multiple windows i.e. when we have to deal with more than 2 windows.

The code snippet for “getWindowHandles()” is given below:

public void explicitWaitForWinHandle(final WebDriver dvr, int timeOut, final boolean close) throws WeblivException

try {
Wait<WebDriver> wait = new WebDriverWait(dvr, timeOut);
ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {

public Boolean apply(WebDriver d) {
int winHandleNum = d.getWindowHandles().size();

if (winHandleNum > 1)
// Switch to new window opened
for (String winHandle : d.getWindowHandles())

// Close the delete window as it is not needed
if (close && dvr.getTitle().equals("Demo Delete Window"))
return true;
return false;

#2) Locating links by linkText() and partialLinkText()

Let us access “” and “” using linkText() and partialLinText() methods of WebDriver.

The above mentioned links can be accessed by using the following commands:



The command finds the element using link text and then click on that element and thus the user would be re-directed to the corresponding page.

The above-mentioned links can also be accessed by using the following commands:



The above two commands find the elements based on the substring of the link provided in the parenthesis and thus partialLinkText() finds the web element with the specified substring and then clicks on it.

#3) Selecting multiple items in a drop dropdown

There are primarily two kinds of dropdowns:

  1. Single select dropdown: A drop-down that allows only single value to be selected at a time.
  2. Multi-select dropdown: A drop-down that allows multiple values to be selected at a time.

Consider the HTML code below for a dropdown that can select multiple values at the same time.

<select id="SelectID_One" multiple="">
<option value="redvalue">Red</option>
<option value="greenvalue">Green</option>
<option value="yellowvalue">Yellow</option>
<option value="greyvalue">Grey</option>

The code snippet below illustrates the multiple selections in a drop down.

// select the multiple values from a dropdown
Select selectByValue = new Select(driver.findElement("SelectID_One")));

#4) Submitting a form

Most or almost all the websites have forms that need to be filled and submitted while testing a web application. The user may come across several types of forms like Login form, Registration form, File Upload form, Profile Creation form etc.

In WebDriver, a user is leveraged with a method that is specifically created to submit a form. The user can also use click method to click on the submit button as a substitute to submit button.

Check out the code snippet below against the above “new user” form:

// enter a valid username

// enter a valid email address      

// enter a valid password

// re-enter the password

// submit the form

Thus, as soon as the program control finds the submit method, it locates the element and triggers the submit() method on the found web element.

#5) Handling iframes

While automating web applications, there may be situations where we are required to deal with multiple frames in a window. Thus, the test script developer is required to switch back and forth between various frames or iframes for that matter of fact.

An inline frame acronym as iframe is used to insert another document within the current HTML document or simply a web page into another web page by enabling nesting.

Consider the following HTML code having the iframe within the webpage:

<head><title>Software Testing Help - iframe session</title>
<iframe id="ParentFrame">
<iframe id="ChildFrame">
<input type="text" id="Username">UserID</input>
<input type="text" id="Password">Password</input>
<button id="LogIn">Log In</button>

The above HTML code illustrates the presence of an embedded iframe into another iframe. Thus, to be able to access the child iframe, a user is required to navigate to the parent iframe first. After performing the required operation, a user may be required to navigate back to the parent iframe to deal with the other element of the webpage.

It is impossible if a user tries to access the child iframe directly without traversing to the parent iframe first.

Select iframe by id

driver.switchTo().frame(ID of the frame);

Locating iframe using tagName

While locating an iframe, the user might face some trouble if the iframe is not attributed with standard properties. It becomes a complex process to locate the frame and switch to it. To buckle down the situation, a user is leveraged to locate an iframe using a tagName method similar to the way we find any other web element in WebDriver.


The above command locates the first web element with the specified tagName and switches over to that iframe. “get(0) is used to locate the iframe with the index value.” Thus, in lines with our HTML code, the above code syntax would lead the program control to switch to “ParentFrame”.

Locating iframe using the index:

a) frame(index)


b) frame(Name of Frame)

driver.switchTo().frame(“name of the frame”);

c) frame(WebElement element)

Select Parent Window


The above command brings the user back to the original window i.e. out of both the iframes.

#6) close() and quit() methods

There are two types of commands in WebDriver to close the web browser instance.

a) close(): WebDriver’s close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver. The command neither requires any parameter nor does it return any value.

b) quit(): Unlike close() method, quit() method closes down all the windows that the program has opened. Same as close() method, the command neither requires any parameter nor does it return any value.

Refer the below code snippets:

driver.close(); // closes only a single window that is being accessed by the WebDriver instance currently

driver.quit(); // closes all the windows that were opened by the WebDriver instance

#7) Exception Handling

Exceptions are the conditions or situations that halt the program execution unexpectedly.

Reasons for such conditions can be:

Thus, to deal with these unexpected conditions, exception handling was conceptualized.

With respect to Java code that we implement while automating a web application can be enclosed within a block that that is capable of providing a handling mechanism against the erroneous conditions.

Catching an exception

To catch an exception, we use the below block of code

       // Protected block
       // implement java code for automation
catch (ExceptionName e)
// catch block - Catches the exceptions generated in try block without halting the program execution

If any exception occurs in the try block/protected block, then the execution controls checks for a catch block for the matching exception type and passes the exception to it without breaking the program execution.

Multiple Catch Blocks

       //Protected block
catch (ExceptionType1 e)
// catch block
catch (ExceptionType2 e)
// catch block
catch (ExceptionType3 e)
// catch block

In the above code, an exception is likely to be caught in the first catch block if the exception type matches. If the exception type does not match, then the exception is traversed to the second catch block and third catch block and so on until the all catch blocks are visited.

WebDriver conditions and Exception Handling

When we aspire to verify the presence of any element on the webpage using various WebDriver ‘s conditional commands, WebDriver presumes the web element to be present on the web page. If the web element is not present on the web page, the conditional commands throw a “NoSuchElementPresentException”. Thus to avoid such exceptions from halting the program execution, we use Exception Handling mechanisms. Refer the code snippet below:

WebElement saveButton = driver.findElement("Save"));
catch(NoSuchElementException e){


In this tutorial, we introduced various WebDriver’s commonly and excessively used commands. We tried to explain the commands with the suitable examples and code snippets.

Next Tutorial #18: In the upcoming tutorial, we would discuss about Web tables, frames and dynamic elements which are an essential part of any web project. We will also cover the exception handling the important topic in more details in one of the upcoming Selenium Tutorials.

Note for the Readers: Till then, stay tuned and automate the web pages and use the above-documented commands.