How to execute javascript in webdriver using javaScriptExecutor using executescript method

JavaScriptExecutor can be used to execute javascript in WebDriver. JavaScriptExecutor actions like the way Xpath, Link, CSS, Id locators works in Selenium Webdriver.

javaScriptExecutor is supported by Selenium library and for that you need to import ‘org.openqa.selenium.JavascriptExecutor ‘ in your program.

In Selenium, javaScriptExecutor works like an interface and facilitates Javascript execution in there.

JavaScriptExecutor has two of the method is as below,

1) executescript – This method will block the execution of next line of code unitl it is completed.


JavascriptExecutor js = (JavascriptExecutor) driver;  



Example for executeScript

import org.openqa.selenium.JavascriptExecutor;    

import org.openqa.selenium.By;   

import org.openqa.selenium.WebDriver;   

import org.openqa.selenium.WebElement;   

import org.openqa.selenium.firefox.FirefoxDriver;   

import org.testng.annotations.Test;   


public class ExecuteScriptExample {   


public void ExecuteScriptTest()



    WebDriver driver= new FirefoxDriver();   

    JavascriptExecutor js = (JavascriptExecutor)driver;   

    driver.get(“your website url having login option”);   

    WebElement button =driver.findElement(“xxxxxxxx”));   



    js.executeScript(“arguments[0].click();”, button);

    js.executeScript(“alert(‘Test is completed – welcome to home page’);”);   




1 Comment

Comments are closed.