Efficient Selenium Scripting and Troubleshoot Scenarios – Selenium Tutorial #27

In the previous tutorial, we discussed the technical implications while implementing logging in a framework. We discussed log4j utility at length. We discussed the basic components those constitute log4j from a usability perspective. With the Appenders and layouts, a user is leveraged to choose the desired logging format/pattern and the data source/location.

In the current 27th tutorial in this comprehensive free selenium online training series, we would shift our focus towards a few trivial yet important topics that would guide us troubleshoot some recurrent problems. We may or may not use them in daily scripting but they would be helpful in the long run.

We would discuss some advance concepts wherein we would deal with mouse and keyboard events, accessing multiple links by implementing lists. So why not let’s just start and briefly discuss these topics with the help of appropriate scenarios and code snippets.

Efficient Selenium Scripting and Troubleshoot Scenarios

JavaScript Executors

While automating a test scenario, there are certain actions that become an inherent part of test scripts.

These actions may be:

  • Clicking a button, hyperlink, etc.
  • Typing in a text box
  • Scrolling Vertically or Horizontally until the desired object is brought into view
  • And much more

Now, it is evident from the earlier tutorials that the best way to automate such actions is by using Selenium commands.

But what if the selenium commands don’t work?

Yes, it is absolutely possible that the very basic and elementary Selenium Commands don’t work in certain situations.

That said, to be able to troubleshoot such situation, we shoulder JavaScript executors into the picture.

What are JavaScript Executors?

JavaScript Executors

JavascriptExecutor interface is a part of org.openqa.selenium and implements java.lang.Object class. JavascriptExecutor presents the capabilities to execute JavaScript directly within the web-browser. To be able to execute the JavaScript, certain mechanisms in the form of methods along with a specific set of parameters are provided in its implementation.


executeScript (String script, args)

As the method name suggests, it executes the JavaScript within the current window, alert, frame etc (the window that the WebDriver instance is currently focusing on)

executeAsyncScript (String script, args)

As the method name suggests, it executes the JavaScript within the current window, alert, frame etc (the window that the WebDriver instance is currently focusing on)

The parameters and import statement are common to both the executor methods.

Script – the script to be executed
Argument – the parameters that the script requires for its execution (if any)

Import statement
To be able to use JavascriptExecutors in our test scripts, we need to import the package using the following syntax:

import org.openqa.selenium.JavascriptExecutor;

Sample Code

#1) Clicking a web element

// Locating the web element using id
WebElement element = driver.findElement(By.id("id of the webelement"));

// Instantiating JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;

// Clicking the web element
js.executeScript("arguments[0].click();", element);

#2) Typing in a Text Box

// Instantiating JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;

// Typing the test data into Textbox
js.executeScript("document.getElementById(‘id of the element’).value=’test data’;”);

#3) Scrolling down until the web element is in the view

WebElement element=driver.findElement(By.xpath("//input[contains(@value,'Save')]"));

// Instantiating the javascriptExecutor and scrolling into the view in the single test step

You may find various other ways of writing the code for accessing JavascriptExecutors.

Accessing multiple elements in a List

At times, we may come across elements of the same type like multiple hyperlinks, images etc arranged in an ordered or unordered list. Thus, it makes absolute sense to deal with such elements by a single piece of code and this can be done using WebElement List. Refer the screenshot below to understand the elements I am talking about.

Selenium scripting tips 1

In the above image, we see that the various service providers belong to a unordered list. Thus, verification of clickability and visibility of these elements can be done by a single piece of code by using a list of elements.

Import statement
To be able to use a WebElement list in our test scripts, we need to import the package using the following syntax:

import java.util.List;

Sample Code

// Storing the list
List <WebElement> serviceProviderLinks = driver.findElements(By.xpath("//div[@id='ServiceProvider']//ul//li"));

// Fetching the size of the list
int listSize = serviceProviderLinks.size();
for (int i=0; i<listSize; i++)

// Clicking on each service provider link

// Navigating back to the previous page that stores link to service providers

There are various requirements under which the lists can be used to verify the elements with suitable implementation changes.

Handling keyboard and mouse events

Handling Keyboard Events

As also said earlier, there are n numbers of ways to deal with the same problem statement in different contexts.

Thus, at times a necessity arises to deal with a problem by changing the conventional dealing strategy with a more advanced strategy. I have witnessed cases where I could not deal with alerts and pop up etc. by selenium commands thus I had to opt for different java utilities to deal with it using keyboard strokes and mouse events.

Robot class is one such option to perform keyboard events and mouse events.

Let us understand the concept with the help of a scenario and its implementation.


Let us gather a situation where an unnecessary pop up appears on the screen which cannot be accepted or dismissed using Alert Interface, thus the only wise option we are left with is to close down the window using shortcut keys – “Alt + spacebar + C”. Let us see how we close the popup using Robot Class.

Before, initiating the implementation, we should import the necessary package to be able to use Robot class within our test script.

Import Statement

import java.awt.Robot;

Sample Code

// Instantiating Robot class
Robot rb =new Robot();

// Calling KeyPress event

// Calling KeyRelease event

Robot class can also be used to handle mouse events but let us here look at the selenium’s capabilities to handle mouse events.

Handling Mouse Events

WebDriver offers a wide range of interaction utilities that the user can exploit to automate mouse and keyboard events. Action Interface is one such utility which simulates the single user interactions.

Thus, we would witness Action Interface to mouse hover on a drop down which then opens a list of options in the next scenario.


  1. Mouse Hover on the dropdown
  2. Click on one of the items in the list options

Import Statement

import org.openqa.selenium.interactions.Actions;

Sample Code

// Instantiating Action Interface
Actions actions=<strong>new</strong> Actions(driver);

// howering on the dropdown
actions.moveToElement(driver.findElement(By.<em>id</em>("id of the dropdown"))).perform();

// Clicking on one of the items in the list options
WebElement subLinkOption=driver.findElement(By.id("id of the sub link"));


In this tutorial, we discussed some advanced topics related to efficient scripting and to troubleshoot scenarios where the user is required to handle mouse and keyboard events. We also discussed how to store more than one web element in a list. I hope you would be able to troubleshoot these impediments if encountered.

Next Tutorial #28: For the upcoming tutorial in the series, we would discuss the concept of Database testing using Selenium WebDriver. We would witness the mechanism of database connection, hitting selenium queries and fetching the results through Selenium WebDriver Code.

Recommended Reading

3 thoughts on “Efficient Selenium Scripting and Troubleshoot Scenarios – Selenium Tutorial #27”

  1. What is the difference between executeScript (String script, args) and executeAsyncScript (String script, args), both seems to me same as per description provided.

  2. The explanations of the methods executeScript and executeAsynScript are exactly the same.

    The Selenium documentation states this is the difference:
    Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.


Leave a Comment