Blog

JAVASCRIPT EXECUTOR IN SELENIUM

INTRODUCTION

Whenever we use selenium WebDriver ,we normally interact with webelements using methods like click(),sendkeys(),findElements() e.t.c .Sometimes WebDriver is not able to interact with certain elements  due to following reasons like the element is hidden or element is overlapeed with other element or the page has dynamic content (like popups) .In such cases selenium provides a special interface called JavascriptExecutor.

What Is JavascriptExecutor
JavascriptExecutor is an interface that allows us to execute javascript code inside the browser.This is mainly used when webdriver cannot handle elements using webdriver methods.

 JavascriptExecutor implements Remote WebDriver class.

JavascriptExecutor allows to run javascript code directly inside browser because every browser understands javascript  So, JavascriptExecutor perform actions that WebDriver cannot handle.

Declaration Of Javascript Executor

  • JavascriptExecutor js=(JavascriptExecutor)driver;

We typecast the WebDriver Object to JavascriptExecutor because WebDriver doesnot directly have executeScritpt() and executeAsyncScript().

Scenarios Where we can use JavascriptExecutor
1. Selenium WebDriver can find elements and can be able to perform operations only if element is visible on the webpage.If we want to perform actions on the elements which are not visible on the webpage in such cases we can use JavascriptExecutor.

2.If we want to highlight elements on the webpage then we can use JavascriptExecutor(Selenium WebDriver doesnot have any method to highlight).

3.To Perform Scrolling operations we can use JavascriptExecutor (Selenium WebDriver doesnot have any method to scroll).
Methods in Javascript Executor

JavascriptExecutor provides following methods like ExecuteScript() and ExecuteAsyncScript()

ExecuteScript()
Execute Script method is used to execute javascript within the selected window or frame.
This method returns data in the form of List,String,Boolean,WebElement .

Syntax: Object  variablename=js.executeScript(String script,  args);

Returns Object (can be String, Long, Boolean, WebElement, etc.)
script refers to javascript code to execute.
 ExecuteAsyncScript()
This method used to handle asynchronous javascript operations like handling AJAX calls ,load content.

Syntax: Object variablename = js.executeAsyncScript(String script, args);

Finding Elements Through Javascript
Elements can be found through javascript using following methods:
ID,name,classname,TagName,CSS Selector ,Xpath

1.Id: document.getElementById(“idname”);
2.Name : document.getElementsByName(“name”);

   Name returns list of elements[Node list].

3.ClassName: document.getElementsByClassName(“classname”);
      ClassName return type is HTML Collection

4.TagName: document.getElementsByTagName(“h3”);
       TagName return type is HTML Collection
Indexing can be applied if we want to get particular element : document.getElementByTagName(“Tagname”)[2];

5.querySelector() : we can pass CSS slector in querySelector()
  ex : document.querySelector(“#id”);

6.evaluate() : we can pass xpath in this method
syntax:document.evaluate(xpathExpression,contextNode,namespaceResolver,resultType,result).singleNodeValue;

contextNode refers to from where we want to search xpath expression like from root element or parent element.

NamespaceResolver is used when we write xpaths on XML documents.

EX:document.evaluate(“//input[@id=’123’],document,null,XPathResult.Number_Type,null).singleNodeValue;

Highlighting Elements Through JavascriptExecutor

Software

Performing click and sendkeys operations through JavascriptExecutor

Software

Scrolling Operations Using JavascriptExecutor

Software