JavascirptExecutor in Selenium

JavascirptExecutor is an Interface. Located inside the package org.openqa.selenium. It has two methods. One is executeScript another one is executeAsyncScript .

Method executeScript:-

Object executeScript(String script, Object… args);

So this is how the method is defined in the source code. This method is abstract and public in nature as it is mentioned inside the Interface, which is implicitly abstract in nature.

Return Type:-

Here the return type is Object. That means it has a long range of return type. It can return WebElement for an HTML element, or can return Boolean for Boolean. So Please look at the below mentioned table for the return type.

Return Types

Arguments:-

Here one argument is String type and another one is varargs (Can receive multiple String arguments or no String Arguments).

When to use JavascriptExecutor :-

When Selenium Library Functions don’t work, then this powerful JavascriptExecutor interface can be used. It can do a lots of extra activities like

  • get an element’s text or attributes.
  • find an element and doing some operations on it like Click().
  • change the color or any other attributes of an element
  • zoom in the page
  • scroll to an element
  • wait until the page is loaded