Sometimes, normal click actions in Selenium do not work because of hidden elements or overlays. Clicking via JavaScript helps to click elements directly using code.
When a button is hidden behind another element and normal click fails.
When a web page uses custom controls that block Selenium's click.
When you want to speed up clicking without waiting for Selenium's checks.
When testing a page with complex animations that interfere with clicks.
This program opens a webpage, finds a button that might be hidden or blocked, and clicks it using JavaScript. It prints a success message if the click runs.
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClickViaJSExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
WebElement button = driver.findElement(By.id("hiddenButton"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", button);
System.out.println("Clicked the button via JavaScript successfully.");
} finally {
driver.quit();
}
}
}
OutputSuccess
Important Notes
Clicking via JavaScript bypasses normal Selenium checks, so use it only when regular clicks fail.
Make sure the element is present and interactable before clicking to avoid errors.
Summary
Clicking via JavaScript helps when normal clicks do not work.
Use JavascriptExecutor and executeScript with arguments[0].click().
It is a useful trick but should be used carefully.
Practice
(1/5)
1. What is the main reason to use JavaScript click in Selenium WebDriver instead of the normal click() method?
easy
A. To automatically scroll the page to the bottom
B. To click elements that are hidden or not responding to normal clicks
C. To avoid using locators for elements
D. To speed up the test execution by skipping waits
Solution
Step 1: Understand normal click limitations
Normal click() may fail if element is hidden or overlapped.
Step 2: Role of JavaScript click
JavaScript click forces the click event on the element regardless of visibility or overlays.
Final Answer:
To click elements that are hidden or not responding to normal clicks -> Option B
Quick Check:
JavaScript click helps when normal click fails [OK]
Hint: Use JS click when normal click throws ElementNotInteractableException [OK]
Common Mistakes:
Thinking JS click speeds up tests
Believing JS click removes need for locators
Assuming JS click scrolls page automatically
2. Which of the following is the correct syntax to perform a JavaScript click on a WebElement named element in Selenium Java?
easy
A. ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
B. driver.executeScript("arguments[0].click();", element);
C. JavascriptExecutor.executeScript("element.click();");
D. driver.javascriptExecutor.execute("arguments[0].click();", element);
Solution
Step 1: Cast driver to JavascriptExecutor
In Selenium Java, driver must be cast to JavascriptExecutor to use executeScript.
Step 2: Use correct method and arguments
The method is executeScript with script arguments[0].click(); and the element passed as argument.
Final Answer:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element); -> Option A
Quick Check:
Cast driver + executeScript + arguments[0].click() = A [OK]
Hint: Always cast driver to JavascriptExecutor before executeScript [OK]
Common Mistakes:
Calling executeScript directly on driver without cast
Using element.click() inside script string
Wrong method names like javascriptExecutor.execute
3. Given the following code snippet, what will be the output if the button is successfully clicked via JavaScript?