In this tutorial I will explain how to click on WebElement using JavaScriptExecutor. Sometimes click() does not work then you can use JavaScriptExecutor to click button or WebElement.
Click WebElement Using JavaScriptExecutor
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Training { static WebDriver driver; public static void main(String[] args) throws InterruptedException { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http:// www.google .com/"); driver.findElement(By.name("q")).sendKeys("http://reditblog.blogspot.in"); Thread.sleep(5000); WebElement element1 = driver.findElement(By.name("btnK")); JavascriptExecutor executor1 = (JavascriptExecutor) driver; executor1.executeScript("arguments[0].click();", element1); Thread.sleep(5000); WebElement element2 = driver.findElement(By.linkText("Reditblog")); JavascriptExecutor executor2 = (JavascriptExecutor) driver; executor2.executeScript("arguments[0].click();", element2); Thread.sleep(5000); driver.quit(); } }