How to Force Selenium WebDriver to Click on Element which is Not Currently Visible?
Last Updated :
28 Apr, 2025
A programming language that is used to execute a set of instructions that a computer can understand and execute to perform various tasks is known as Java. Java can be controlled autonomously using various automation tools.
Selenium is one such tool, that can make the work of developer and tester easier. There are various instances when the user needs to click or perform a certain action on the element that is currently not in the scope of visibility. In this article, we will see how we can forcefully click on an element that is not currently visible using Selenium webdriver.
Visibility Criteria
The conditions that decide whether the element is visible or hidden within the viewport are known as visibility criteria. For performing any action on the element in the web page, either you need to make that element appear within the viewport or forcefully click that element.
Below are the different methods of forcing Selenium WebDriver to click on the element that is not currently visible:
1. Recommended Approaches
- Wait for Visibility
- Scroll the Element into View
- Address Underlying Issues
2. Less Recommended Approach
- Forceful Click using JavaScript
Wait for Visibility
There are certain scenarios in which the element becomes visible after performing certain actions. In such cases, the user can let Selenium webdriver wait until the element is visible. This can be done using ExpectedConditions.visibilityOfElementLocated function. In this approach, we will see how we can wait for the visibility of the element before clicking on it.
Syntax:
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(seconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(element_text)));
Here,
seconds: These are the seconds for which we want the user to wait for the visibility of the element.
element_text: It is the text for which the webdriver needs to wait before clicking on that element.
Example:
In this example, we have opened the Geeks For Geeks website (link) and then waited until the element containing the text 'Trending Now' is visible. Once it is visible, we have clicked on that element.
Java
//Importing the Selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import org.openqa.selenium.By;
public class selenium3 {
public static void main(String[] args) {
// Specify the location of the driver
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Initialising the driver
WebDriver driver = new ChromeDriver();
// Launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Add explicit wait of 5 seconds
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5));
// Wait until the element Trending Now is visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Trending Now")));
// Click on the element
driver.findElement(By.linkText("Trending Now")).click();
}
}
Output:

The certain cases where the element is not visible within the viewport, we need to scroll the element into view before performing a specific action on them. This can be done using JavascriptExecutor, which is used to perform the actions that cannot be performed through Selenium in-built functions. In this approach, we will see how we can scroll the element into view and click on that element.
Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);
Here,
element: It is the element that has to be brought into view by scrolling.
Example:
In this example, we have opened the Geeks For Geeks website (link) and then found the element containing the text 'Problem of the day' which is out of the viewport. Then, we scrolled to the element using the retry mechanism till the element was clearly in the viewport and then we clicked that element.
Java
//Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium3 {
public static void main(String[] args) throws InterruptedException {
//specify the location of the driver
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
//Initialising the driver
WebDriver driver = new ChromeDriver();
//Launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Find Problem of the day text
WebElement element = driver.findElement(By.xpath("// *[contains(text(),'Problem of the day')]"));
int maxRetries = 10;
int retryCount = 0;
// Run while loop
while (retryCount < maxRetries) {
try {
// Scroll to the specific position
js.executeScript("arguments[0].scrollIntoView();", element);
// Click that element
if (element.isDisplayed() && element.isEnabled()) {
element.click();
break;
}
} catch (Exception e) {
// Increment retry count if element is not found
retryCount++;
if (retryCount == maxRetries) {
// Throw exception of element is not found
throw e;
} else {
// Sleep for some time
Thread.sleep(10);
}
}
}
}
}
Output:

Address Underlying Issues
- Challenges with scrolling: In most cases, after the Selenium has scrolled to the element, still the element is not visible within the viewport. Thus the webdriver couldn't perform any further action on that element, such as clicking, etc. Thus, we need to implement try-catch until the element is visible within the viewport and further actions can be performed on it.
- Dynamic Loading: In certain cases where the element appears at a certain time, the user doesn't know how much time it will take the element to load, hence Selenium needs to find the element dynamically until the element is loaded. This affects the performance of the script execution.
- Appropriate element state: The user can face the challenges of performing actions on the element if the element is not in the appropriate state, i.e., disable or not clickable.
Forceful Click using JavaScript
In the cases, where above stated approaches don't work, we need to forcefully click on an element using Javascript. This can be done using executeScript and click functions of JavascriptExecutor. In this approach, we will see how we can forcefully click an element using JavascriptExecutor.
1. executeScript: The feature of JavascriptExecutor that enables to execution of JavaScript code within the context of the current browser window is known as executeScript.
2. click(): The function that is used to simulate a mouse click action on a web element is known as the click function.
Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
Here,
element: It is the element that is out of the viewport and you want to click forcefully.
Example:
In this example, we opened the Geeks For Geeks website (link) and then found the element containing the text 'Problem of the day' which is out of the viewport, and then clicked on that element using JavascriptExecutor.
Java
//Importing the Selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium3 {
public static void main(String[] args) {
// Specify the location of the driver
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Initialising the driver
WebDriver driver = new ChromeDriver();
// Launch the website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Stating the Javascript Executor driver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Find Problem of the day text
WebElement element = driver.findElement(By.xpath("// *[contains(text(),'Problem of the day')]"));
// Forcefully click on an element
js.executeScript("arguments[0].click();", element);
}
}
Output:

Conclusion
In conclusion, Javascript execution can help the Selenium web driver forcefully click on an element that is not currently visible. This is one of the best ways to click an element or perform some other action on an element if we are unsure about the visibility of an element. I hope after reading the various approaches mentioned in the article, you will be able to forcefully click on the element that is not currently visible using Selenium Webdriver.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING