The Scroll element in the page is actually a div with scroll bound to it. Here is the HTML snippet.
<div class="slimScrollBar" style="background: none repeat scroll 0% 0% rgb(137, 137, 137); width: 12px; position: absolute; border-radius: 0px; z-index: 99; right: 1px; top: -13px; opacity: 0.6; height: 224.835px; display: block;"/>
I have tried with the following code, but it didn't work out
WebElement scrollArea = driver.findElement(By.cssSelector("div.slimScrollBar"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollTop = arguments[1];",scrollArea, 250);
Thread.sleep(1000);
Help is appreciated!
-
2This is same as/duplicate of sqa.stackexchange.com/questions/9655/…demouser123– demouser1232015年09月23日 10:44:05 +00:00Commented Sep 23, 2015 at 10:44
-
It doesn't worked for me. The problem is, the scroll bar is only visible when the mouse over is performed in the particular div. So its throwing "unknown error: cannot focus element"jass– jass2015年09月24日 05:30:36 +00:00Commented Sep 24, 2015 at 5:30
-
Can you give me an example. I think you can overcome this by using a wait, so that until element is focussed, the webdriver will wait.demouser123– demouser1232015年09月24日 09:36:20 +00:00Commented Sep 24, 2015 at 9:36
-
3Does this answer your question? Unable to scroll down to bottom of div with data loading dynamicallyBharat Mane– Bharat Mane ♦2020年02月20日 13:20:41 +00:00Commented Feb 20, 2020 at 13:20
4 Answers 4
For Scroll down:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
or, you can do as follows:
jse.executeScript("scroll(0, 250);");
For Scroll up:
jse.executeScript("window.scrollBy(0,-250)", "");
OR,
jse.executeScript("scroll(0, -250);");
-
How would you scroll to the exact position of the webelementAshok kumar Ganesan– Ashok kumar Ganesan2020年04月28日 13:11:38 +00:00Commented Apr 28, 2020 at 13:11
You could try this.
WebElement e=driver.findElement(By.xpath("enter xpath which element is in end of the div"));
Coordinates cor=((Locatable)e).getCoordinates();
cor.inViewPort();
Thread.sleep(1000);
-
1Could you give a bit more background to your answer. Why is this solution useful in this situation? What are the pros/cons of this approach? The more detailed context you can give the more the answer will help others.ECiurleo– ECiurleo2016年03月09日 10:09:00 +00:00Commented Mar 9, 2016 at 10:09
-
??? what's this now?IAmMilinPatel– IAmMilinPatel2016年03月09日 13:07:48 +00:00Commented Mar 9, 2016 at 13:07
-
This worked for me. I searched a lot but just this one works. I want to say you are beautiful and amazing. :)Evan Hu– Evan Hu2018年01月09日 05:13:26 +00:00Commented Jan 9, 2018 at 5:13
Some HTML page have internal (custom) scroll bar. We have to handle with little bit different way.
Javascript is not working here.
Solution :
WebElement scrollArea = driver.findElement(By.cssSelector("div.slimScrollBar"));
Create method scroll_Page as given below.
Call this method as scroll_Page(scrollArea ,100);
Where scrollArea is your dragged(scroll) element and 100 is scroll points.
public static boolean scroll_Page(WebElement webelement, int scrollPoints)
{
try
{
Actions dragger = new Actions(driver);
// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 10;
for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
{
dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
}
Thread.sleep(500);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
In Python:
driver.execute_script('document.getElementById("viewport").scrollTop += 100')
In Java:
driver.executeScript('document.getElementById("viewport").scrollTop += 100');
-
Or simply
driver.execute_script('window.scrollBy(X, Y)')
Mr.Coffee– Mr.Coffee2020年07月20日 10:48:34 +00:00Commented Jul 20, 2020 at 10:48 -
Yes, this works fine until you try to scroll in a certain item that has its own scroll bar.Justin Iven Müller– Justin Iven Müller2020年07月21日 14:12:12 +00:00Commented Jul 21, 2020 at 14:12