Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Part 2: Usages Of Javascripts In Selenium: How To Run Javascript Commands In Selenium

Posted on 03/21/2025 By admin

Hello folks,

In previous post, we have learnt why do we need javascript in Selenium webdriver.  In this post we will learn how to run javascript commands in selenium webdriver.

Selenium people provides an interface named “JavascriptExecutor“, which provides declaration of two methods as below:

  1. Object executeScript(String arg0, Object… arg1);
  2. Object executeAsyncScript(String arg0, Object… arg1);

This interface enable a browser to execute javascript command using above methods.

RemoteWebDriver implements JavascriptExecutor and provides definition to both methods. All browser driver classes like ChromeDriver etc extends RemoteWebDriver , which enables to execute Javascript commands in selenium script.

Let’s learn more basics:

I see people who just downcast a driver object to execute javascript command without knowing whether it is required and they get stuck.

You must need to know the hierarchy of classes of interfaces in Selenium webdriver.

At high level, WebDriver is super interface in hierarchy and RemoteWebDriver is fully implemented classes of it. Along with WebDriver , RemoteWebDriver implements JavascriptExecutor interface as well. Now, let’s see different scenarios:

Case 1: 

If you create a browser driver class object and up cast it to WebDriver as below:

WebDriver driver= new ChromeDriver();

In this case we need to down cast “driver” to “JavascriptExecutor”  to execute javascript commands.

Reason of down casting is that, “WebDriver” interface has no visibility of methods of “JavascriptExecutor” (executeScript and executeAsyncScript). So to execute javascript command in this case, you must need to down cast driver.

Example code:

Output: You will see “Hello” printed in browser console.

Uncategorized

Post navigation

Previous Post: api
Next Post: Postman Tutorial Part 36 – Export & Import Postman Collection As a JSON – Sharing Postman Collection

Related Posts

How To Type, Retrieve and Clear values from the Text box and Text Area in Selenium WebDriver Uncategorized
October 2, 2018 – Make Selenium Easy Uncategorized
Agile Software Development – Let’s Understand It Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
October 20, 2018 – Make Selenium Easy Uncategorized
All About getText() method: What, Why And How. | Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark