SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

JavaScriptExecutor in Selenium WebDriver, Methods with Examples

In this Selenium JavaScriptExecutor tutorial, we will learn the following

What is JavaScriptExecutor?

JavaScript executor is an interface that gives a mechanism to execute JavaScript through Selenium WebDriver. It provides two methods such as “executescript” & “executeAsyncScript” to run javascript on the currently selected frame or window or page.

Why we use JavaScriptExecutor?

There are locators in Selenium WebDriver like ID, Class, XPath, etc., to work with elements on a web page. Sometimes these default Selenium locators may not work. Here comes the JavaScriptExecutor in the picture.

JavaScriptExecutor is used to perform operations on a web page. To use JavaScriptExecutor in Selenium scripts there is no need to install an addon or plugin. The only step we need to take is to import org.openqa.selenium.JavascriptExecutor in the Selenium script.

In general, we do click on an element using the click() method in Selenium.

For example:

Sometimes web controls don’t react well against Selenium commands and we may face issues with the above statement “click()“. As said earlier, to overcome such kind of situation, we use the JavaScriptExecutor interface.

There is no need to write a separate script to execute JavaScript within the browser using the Selenium WebDriver script. Just use a predefined interface named ‘Java Script Executor‘.

We need to import the below package in the script.

Package:

Syntax:

Script – The JavaScript to execute
Arguments –
The arguments to the script(Optional). May be empty.
Returns –
One of Boolean, Long, String, List, WebElement, or null.

Methods in JavaScriptExecutor

1. executeAsyncScript
2. executeScript

executeAsyncScript

This method executes an asynchronous piece of JavaScript in the context of the currently selected window or frame in Selenium. Asynchronous script renders your webpage fastly.

executeScript

This method executes JavaScript in the context of the currently selected window or frame in Selenium.

How We Use JavaScriptExecutor in Selenium

Let’s see some scenarios we could handle using JavaScriptExecutor Interface:

To Type Text in a Text Box

To type Text in Selenium WebDriver without using sendKeys() method

To Click on a Button

To click a Button in Selenium WebDriver using JavaScript

To Handle Checkbox

We pass true or false to handle the checkbox

To generate Alert Pop window in selenium

To refresh browser window using Javascript

To get innertext of the entire webpage in Selenium

To get the Title of our webpage

To get the domain

To get the URL of a webpage

To perform Scroll on an application using  Selenium

See Also: How To Scroll Web Page Down Or UP Using Selenium WebDriver

To click on a SubMenu which is only visible on mouse hover on Menu

To navigate to a different page using Javascript

To find a hidden element in selenium using JavaScriptExecutor

Practical Example using Selenium JavaScriptExecutor

The below program will guide you to handle some of the scenarios we do use while writing scripts.

Practical Example using executeAsyncScript

executeAsyncScript renders your webpage faster. Using executeAsyncScript, our script runs by the browser side and not by the server side because it sends a callback to the server side testing suite once the script is ready.

Don’t miss: What’s new in Selenium 4

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar

Leave a Comment

API Testing eBook

DOWNLOAD FOR FREE