WebDriver click() vs JavaScript click().

In Selenium WebDriver, there are two primary methods to click elements: the standard WebDriver click() method and JavaScript click() through JavaScriptExecutor. Each approach has distinct advantages and use cases.

WebDriver click() Method

The standard WebDriver click() simulates actual user interaction. It waits for the element to be visible and clickable before performing the action. This method works with various locators including link text and partial link text.

WebDriver click() Browser Engine DOM Element User-like interaction flow Waits for element visibility and clickability

Example: WebDriver Click

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

public class DriverClick {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
        
        // identify link with link text locator
        driver.findElement(By.linkText("Write for us")).click();
        System.out.println("Page title after click: " + driver.getTitle());
        
        driver.quit();
    }
}

JavaScript click() Method

JavaScript click() executes directly through the browser's JavaScript engine, bypassing WebDriver's interaction checks. This method is faster but doesn't simulate real user behavior.

JavaScript Executor DOM Element Direct JavaScript execution Bypasses visibility and interaction checks

Example: JavaScript Click

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

public class DriverClickJs {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
        
        // identify link
        WebElement element = driver.findElement(By.linkText("Write for us"));
        
        // click link with JavaScript Executor
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", element);
        System.out.println("Page title after click: " + driver.getTitle());
        
        driver.quit();
    }
}

Comparison

Aspect WebDriver click() JavaScript click()
User Simulation Yes - mimics real user No - programmatic only
Visibility Check Yes - waits for visibility No - clicks hidden elements
Speed Slower Faster
Use Case Standard interactions Hidden/overlapped elements

When to Use Each Method

Use WebDriver click() when:

  • Testing normal user interactions
  • Element is visible and accessible
  • You want realistic test scenarios

Use JavaScript click() when:

  • Element is hidden or overlapped
  • WebDriver click() fails due to interaction issues
  • You need faster execution for non-UI critical tests

Conclusion

WebDriver click() provides realistic user interaction simulation, while JavaScript click() offers direct DOM manipulation. Choose WebDriver click() for standard scenarios and JavaScript click() when dealing with complex UI elements or when standard clicks fail.

Updated on: 2026-03-15T23:19:00+05:30

830 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements