- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to scroll a specific DIV using Selenium WebDriver with Java?
We can scroll a specific DIV using Selenium webdriver. Selenium cannot handle scrolling directly. It takes the help of the Javascript Executor to do scrolling action to a specific DIV.
First of all we have to identify the specific DIV up to which we have to scroll to with the help of xpath or css locator. Next we shall take the help of the Javascript Executor to run the Javascript commands. The method executeScript is used to execute Javascript commands in Selenium. We have to use the scrollIntoView method in Javascript and pass true as an argument to the method.
Syntax
WebElement m=driver.findElement(By.xpath("//div[@class='slick-track']")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", m);
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ScrollToDiv{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // identify element WebElement m=driver.findElement(By.xpath("//div[@class='slick-track']")); // Javascript executor ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", m); Thread.sleep(800); driver.quit(); } }
Output
- Related Articles
- How to scroll down using Selenium WebDriver with Java?
- How to scroll to element with Selenium WebDriver using C#?
- How can I close a specific window using Selenium WebDriver with Java?
- How to scroll the Page up or down in Selenium WebDriver using java?
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How can I scroll a web page using selenium webdriver in python?
- How to Scroll Down or UP a Page in Selenium Webdriver?
- How to scroll down a webpage in selenium using Java?
- How to type in textbox using Selenium WebDriver with Java?
- How to get selected option using Selenium WebDriver with Java?
- How to handle authentication popup with Selenium WebDriver using Java?
- How can I select date from a datepicker div using Selenium Webdriver?
- Switch tabs using Selenium WebDriver with Java.
- How to select checkboxes using selenium java webdriver?
- How to set a cookie to a specific domain in selenium webdriver with Python?

Advertisements