Skip to content
CodeKruPro

Click Using JavaScript

60/70

Click Using JavaScript

Sometimes Selenium's click() fails due to overlapping elements. JavaScript click bypasses these issues.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the leftClick button
  3. Click it using JavaScript instead of Selenium's click()
  4. Print the button text after the JS click

Expected output:

After JS click: I was left-clicked!

About this lesson

Click an element with JavaScript in Selenium Java

Learn Selenium JavaAdvancedJavaScript Executor

How it works

A script that calls click() on arguments[0], with the element passed in, runs the DOM's own click method on that element. It fires a click event and nothing else: in this runner a WebDriver click on the left-click button fires pointerdown, mousedown, focus, pointerup, mouseup and click, while the JavaScript click fires only click.

It skips every check a user would hit. With a full-page overlay injected over the Buttons page, click() throws ElementClickInterceptedException and names the overlay as the element that would receive the click. The JavaScript click goes through anyway and the button's label changes.

That is exactly why it is a last resort. A user cannot click through an overlay, so a test that does may pass on a page that is broken for real people. The DOM still respects disabled controls, though: a JavaScript click on disabledButton does nothing.

About the assignment

The starter gives you the executor and the left-click button. You click the button through JavaScript instead of WebDriver and print its label to see that the page's click handler ran.

What you will do

  1. Call js.executeScript() with a script that calls click() on arguments[0], and pass the button.
  2. Read the button's text and print it after the label After JS click:.

Common mistakes

  • Using it to hide a real bug

    If click() is intercepted, first ask why. A spinner that never goes away or a misplaced banner is a defect the test should report.

  • Expecting mouse events

    Handlers on mousedown or mouseup do not run, so menus and drag handles that rely on them stay closed.

  • Expecting it to double-click

    A JavaScript click() on the double-click button leaves its label unchanged, because no dblclick event is fired.

In a real test suite

Teams keep JavaScript clicks for cases they understand: an element covered by a decorative layer the user clicks through visually, or a third-party widget with an awkward hit area. Each use is usually commented with the reason.

Lesson 59 scrolled with JavaScript. Lesson 61 changes the page itself, making a hidden element visible.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement button = driver.findElement(By.id("leftClick"));
        
        // TODO: Click using JavaScript
        // TODO: Print button text after click
        
        driver.quit();
    }
}