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
- Call
js.executeScript()with a script that callsclick()onarguments[0], and pass the button. - 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
mousedownormouseupdo 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 nodblclickevent 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.
Related lessons
- Click a ButtonThe normal
click(), and the exceptions it throws. - Wait for an Element to DisappearWait for an overlay to disappear instead of clicking through it.
- JavaScript Executor - Get ValuesHow
executeScript()passes the element in.
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.
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();
}
}