Click element using JavaScriptExecutor

In Selenium Webdriver, we can just use element.click() method to click on any element. But sometimes, when there are any issues performing click on any element, we can use JavaScriptExecutor.

Below is the example to perform click using JavaScriptExecutor.

package com.pack.click;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;


public class JSClickExample {
	WebDriver driver;

	@Test
	public void testClickJS() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.navigate().to("http://www.amazon.com/");
		WebElement searchTextBox=driver.findElement(By.id("twotabsearchtextbox"));
		searchTextBox.sendKeys("Mobiles");
		WebElement goButton = driver.findElement(By.cssSelector("input[value=Go]"));
		safeJavaScriptClick(goButton);
	}
	
	public void safeJavaScriptClick(WebElement element) throws Exception {
		try {
			if (element.isEnabled() && element.isDisplayed()) {
				System.out.println("Clicking on element with using java script click");

				((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
			} else {
				System.out.println("Unable to click on element");
			}
		} catch (StaleElementReferenceException e) {
			System.out.println("Element is not attached to the page document "+ e.getStackTrace());
		} catch (NoSuchElementException e) {
			System.out.println("Element was not found in DOM "+ e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to click on element "+ e.getStackTrace());
		}
	}
}
Selenium Tutorials: 

Comments

nice stuff

good

In Selenium Webdriver, we can just use element.click() method to click on any element. But sometimes, when there are any issues performing click on any element, we can use JavaScriptExecutor.
Good job mr

Thanks --Selenium Easy

Code is working fine

JavascriptExecutor js=(JavascriptExecutor) driver; I didn't understand this line, JavascriptExecutor is an interface, so we are casting driver object to JavascriptExecutor??

you said "But sometimes, when there are any issues performing click on any element, we can use JavaScriptExecutor". i want to know what are the possible issue while performing simple click.
And i also wants to know how javascript click works i.e i want to know "JavaScriptExecutor click" mechanism.

thanks in Advance :)

Excellent stuff

One thing worth mentioning is that JavascriptExecutor will click the element even if it's not visible to the end user in real live situation. Is this something that we really want to test, i.e. an element that possibly is not clickable because of the layout issue or any other reason ? Just wondering.

i Think ,That's the reason we are checking for element visibility as a condition to process inside...:)

Add new comment