Scroll the Element into View
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 // 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:
How to Force Selenium WebDriver to Click on Element which is Not Currently Visible?
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.
Table of Content
- Visibility criteria
- Wait for Visibility
- Scroll the Element into View
- Address Underlying Issues
- Forceful Click using JavaScript
- Conclusion
- FAQs
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.