I have a div element that I want to scroll down inside it with selenium.
I want to scroll specific pixels.
What is the easiest way to do it?
I searched on web but didn't find fine explanation.
Asked
Active
Viewed 1,127 times
1

Prophet
- 32,350
- 22
- 54
- 79

Yonatan Alshech
- 25
- 5
-
Is this div inside a nested window ? How does it look like ? Can you share html code or page url ? – cruisepandey Jul 04 '21 at 13:40
-
https://stackoverflow.com/questions/9707397/making-a-div-vertically-scrollable-using-css like the div here – Yonatan Alshech Jul 04 '21 at 13:42
-
There are multiple ways to scroll, actions class, java script executor, did you try any one of them ? – cruisepandey Jul 04 '21 at 13:45
-
There is an option in actions class to scroll by pixels? I tried the answer Prophet answered here but it throes exception. – Yonatan Alshech Jul 04 '21 at 13:48
1 Answers
1
You can use this method:
public void scrollOnElement(String cssSelector){
JavascriptExecutor js = (JavascriptExecutor)driver;
String script = String.format("document.querySelector('%s').scrollTop= 450",cssSelector);
js.executeScript(script);
}
The css parameter here is the css selector locating the div element you want to scroll inside.
I use 450 pixels scrolling here however you can use any parameter instead.

Prophet
- 32,350
- 22
- 54
- 79
-
-
-
it throwing me exception "javascript error: missing ) after argument list" – Yonatan Alshech Jul 04 '21 at 13:39
-
-
-
I use it in my code and it works perfect! Did you copied all the method as it appearing now? – Prophet Jul 04 '21 at 14:03
-
I wrote in cssSelector "driver.findElement(By.xpath("//div[@class='grid-canvas']"))" – Yonatan Alshech Jul 04 '21 at 14:10
-
This is not css selector! in this case the css selector is: `div.grid-canvas` – Prophet Jul 04 '21 at 14:15