Critical 12 javascriptexecutor methods in selenium

In this segment of the tutorial we are going to discuss javascriptexecutor in selenium in detailed manner and all the probable operations could be performed using javascriptexecutor in selenium in terms of WebAutomation . 

What is javascriptexecutor in selenium 

In the Selenium Library javascriptexecutor is an interface which is being implemented several classes such as ChromeDriver,FirefoxDriver, RemoteWebDriver,EdgeDriver, EventFiringWebDriver, InternetExplorerDriver etc. to support different Javascript related operations towards the respective browser Drivers.

JavaScriptExecutor in Selenium
JavaScript Executor in Selenium

Types of Java Script in Selenium and its Methods: 

There are two different kinds of JavaScript methods are available : 

Difference between executeAsyncScript vs executeScript :

executeAsyncScript  : This type of java script executor is used to execute an asynchronous JavaScript code in the context of the currently selected frame or window.

executeScript : This type of the Java scriptexecutor which basically executes synchronous JavaScript code in the context of the currently selected frame or window. 

 

Operation That could be performed using  javascript executor in selenium : 

There are various crucial Web operations that is achieved executing Javascript in browser driver to achieve certain operation such as : 

Lets discuss all the operation mentioned above : 

Selenium javascript click 

We can perform click operation in Selenium with Javascript operation in the below approach : 

public void jsClick() {
   WebElement element = driver.findElement(By.xpath("Xpath_OF_Element"));
   JavascriptExecutor jscriptExecutor = (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("arguments[0].click();", element);
}

Selenium javascript enter text

We can perform sendkeys operation in Selenium with Javascript operation in the below approach : 

public void jsSendKeys() {
   JavascriptExecutor jscriptExecutor = (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("document.getElementById('id_of_the_element').value='ENTER_THE_TEXT_HERE';");
}

Selenium javascript check checkbox 

We could use option checked=true or false depending on the use-cases to perform the CheckBox operations with Javascript in Selenium.

public void jsCheckBoxOps() {
   JavascriptExecutor jscriptExecutor = (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("document.getElementById('id_of_the_element').checked=false;");
}

Selenium javascript Alert generation

To generate the alert we need to use “alert(‘ALERT_TEXT_TO_BE_SHOWN’);” while performing the operation with Javascript.

public void jsAlertGeneration() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("alert('ALERT_TEXT_TO_BE_SHOWN');");
}

Selenium javascript refresh page

To refresh a webpage in Selenium with Javascript Executor we can use history.go(0).

public void jsRefreshPage() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("history.go(0)");
}

Selenium javascript to get  inner text of WebPage

To fetch the whole inner text we can use document.documentElement.innerText;

public String jsFetchInnerText() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   return String.valueOf(jscriptExecutor.executeScript("return document.documentElement.innerText;"));
}

Selenium javascript get title of the Page 

To return the title of a Page we can use document.title; along with Javascript Executor in Selenium .

public String jsWebPageTitle() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   return String.valueOf(jscriptExecutor.executeScript("return document.title;"));
}

Selenium javascript to get the Domain 

To pull the domain name we can use document.domain;

public String jsFetchDomain() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   return String.valueOf(jscriptExecutor.executeScript("return document.domain;"));
}

Selenium javascript to get the URL of a webpage

 

To fetch the URL using JavaScript Executor with Selenium we could use document.URL;

public String jsFetchURL() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   return String.valueOf(jscriptExecutor.executeScript("return document.URL;"));
}

Selenium javascript Scrolling a webpage 

We can perform scrolling is multiple ways using Javascript , two of the approaches are shown below in the code snippet such as :

public void scrollIntoView(By locator) {
   try {
       JavascriptExecutor executor = (JavascriptExecutor) driver;
       WebElement element = driver.findElement(locator);
       executor.executeScript("arguments[0].scrollIntoView(true);", element);
       BASE_LOGGER
               .info("scrollIntoView operation has been performed for the locator : " + String.valueOf(element));
   } catch (Exception e) {
       String exceptionData = e.getCause().getMessage();
       BASE_LOGGER.error("Error encountered i.e : " + exceptionData
               + " while performing scrollIntoView on the element : " + locator);
   }
}

Also we can specify the scrolling value(amount of scroll) like this below approach :

public void jsScroll() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("window.scrollBy(0,1000)");
}

Selenium javascript to navigate to a different page

We can navigate to a different webpage/screen by directly providing the url location within the javascriptexecutor in Selenium. 

public void pageNavigationWithJS() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   jscriptExecutor.executeScript("window.location = 'PageURL_To_Navigate'");
}

Selenium javascript To find and click on a hidden element

We could directly click on the hidden element with the Javascript from the backend even though the element is not visible , here is the code snippet for the same:

public void clickOnHiddenElement() {
   JavascriptExecutor jscriptExecutor= (JavascriptExecutor) driver;
   WebElement hiddenElement=driver.findElement(By.id("ID_OF_ELEMENT"));
   jscriptExecutor.executeScript("arguments[0].click();", hiddenElement);
}

Conclusion :

We have so far discussed about the Javascript and various operations to be performed using Javascript executor in Selenium , you are encouraged to go through the full Selenium tutorial to get a better grip on all the different aspects of the same. In the upcoming Segments of Tutorial series we will discuss more on the Selenium Automation Framework and other latest technologies to work with Selenium , and to crack any advanced interview in Selenium you can go through here.

Leave a Comment