Sometimes, your native Selenium call for clicking element is going to fail, in this case, we can go for click operation using JavascriptExecutor.

In this post, I’m taking the Salesforce Login page – to demonstrate the click operation from JavascriptExecutor.

If you want to explore more details about the JavascriptExecutor interface – Please click Here.

Example Program to Click on Web Element using JavascriptExecutor

package swdbasics;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class JsClickExp {

	public static void main(String[] args) throws InterruptedException 
		// inform selenium about driver path
		System.setProperty("", "D:\\browser-drivers\\chromedriver.exe");
		// instantiate driver object
		WebDriver driver = new ChromeDriver();
		// launch aut
		//maximize the site
		// lets wait for some time
		// locate login button
		WebElement we = driver.findElement("Login"));
		//; // selenium way
		// click this button using Javascript
		// cast driver object to Js Executor
		JavascriptExecutor je = (JavascriptExecutor)driver;
		je.executeScript("arguments[0].click();", we);
		//lets wait for some time
		// close the window



❤️Love Automation ❤️