Selenium Page Interactions and Actions API


A Note Before Interactions

Slow Network Connections are evil for Selenium

Since right the after the statement to load the page the element is not displayed, because the page might not have been loaded fully depending upon the network connection speed. If this is the case then the best way to deal with this issue is to wait for some time so that the page loading is completed and the element is properly displayed and is ready to be interacted.

Also sometimes even if the element is present in the page but to make it visible the browser might take some time or the element’s visiblity might depend on some event on some other element, modal boxes are the best examples of this case. If the element is not visible the driver throws the ElementNotVisibleException. The reason why the WebDriver throws this exception is because from a user’s perspective an element cannot be interacted with if it is not visible to the user.

Here by visibility we mean that the CSS “display” property is set to be “none” not the CSS visibility property. For this all we need to do is add an explicit wait condition for the element to be visible.
But there are JavaScript hacks available to interact with element even if it is not visible.

Now since we have seen how to select elements on page we are ready to interact with them. These interactions are just the actions (JavaScript events) what a normal user would like to perform on elements of the page. Below is a list of most common interactions/actions/events that are supported by the Selenium API.


Click is probably the most common action performed by a user. It simulates the same click event that triggers when a user of the web application clicks on any element.
We have already seen several previous examples similar to code below:

WebElement element = driver.findElement("some_id"));;


This method enters a string of text into input fields or text area. Below is an example:

WebElement element = driver.findElement("input_field_id"));
element.sendKeys("Some Text");


The submit() method is used for form elements like an input box. When a submit() method is called on an element the driver traverse upward in the HTML and tries to find a form element and triggers submit on the form. If the form element is not found it throws the NoSuchElementException. Below is an example:

WebElement element = driver.findElement("input_field_id"));


As the name suggests the getText() method returns the text content from the specified element.

WebElement element = driver.findElement("input_field_id"));
String content = element.getText();


At numerous times user might need to scroll down the page to locate some element. For this JavaScriptExecutor class can be utilized. Below is an example of that. The scrollTo() method of window object takes two parameters: 1.) horizontal location from left size of the page (in pixels) and 2.) vertical location from the top of the page(in pixels). In the code below the horizontal argument is set to 0(assuming there is no horizontal scroll in the page) and the vertical argument to element’s vertical position from top.

// Get the element WebElement targetElement = driver.findElement("itemid"));
// Create the JavascriptExecutor object
JavascriptExecutor jse = (JavascriptExecutor)driver;
// Scroll down the page to the element with JavaScript
// Click on the element;

Drop Down/Select Options

Selenium provides a class for the select element and three different methods to choose the option.

Select select = new Select(driver.findElement("element_id")));
//Method 1
select.selectByIndex(1); //0 based indexes
//Method 2
//Method 3

Firing JavaScript Events

Consider following HTML:

<a href="" onclick="doSomeAction();">Link Text</a>

In the above code, since it is an anchor tag, the click event does not fire hence the method the doSomeAction() does not get executed, based on different browsers event handling mechanisms.
To handle this kind of situations, the JavaScriptExecutor can be used.

executor.executeScript("arguments[0].click();", element);

Selenium RC support: Selenium RC provided support for JavaScript firing events, but in Selenium 2.0 the functionality was not included. Here is how it worked in Selenium 1.0:

Selenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "");
selenium.fireEvent("link=Link Text", "click");

AdvancedUserInteractions API

The AdvancedUserInteractions API provides support for more advanced user interaction. It can be a drag and drop event or selecting(clicking) multiple elements while holding the Control key. Below are examples of drag and drop event and the Clicking multiple elements while holding the Control key.

Drag and Drop

To enable an element to be dragged and dropped on some other element the Selenium API provides the dragAndDrop() method from the Actions class.

WebElement elementToBeDragged = driver.findElement("id1"));
WebElement targetElement = driver.findElement("id2"));
(new Actions(driver)).dragAndDrop(elementToBeDragged, targetElement).perform();

Simulating Sequence Of Events

The Actions class can be used to simulate multiple events at a time(for example, selecting multiple files by clicking, while holding the CTRL key). The code below uses multiple events to simulates the same action.

Actions builder = new Actions(driver);
Action selectMultiple =;

Single Actions

Below is a list of the actions supported by the API.

  • ButtonReleaseAction – Releasing a held mouse button
  • ClickAction – Equivalent to
  • ClickAndHoldAction – Holding down the left mouse button
  • ContextClickAction – Clicking the mouse button that (usually) brings up the contextual menu
  • DoubleClickAction – double-clicking an element
  • KeyDownAction – Holding down a modifier key
  • KeyUpAction – Releasing a modifier key
  • MoveMouseAction – Moving the mouse from its current location to another element
  • MoveToOffsetAction – Moving the mouse to an offset from an element (The offset could be negative and the element could be the same element that the mouse has just moved to)
  • SendKeysAction – Equivalent to WebElement.sendKey(…)

We are ready to take a look at the Selenium Explicit and Implicit Waits. Let us know any issues regarding the above topic.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s