Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to scroll down in Chromedriver by selenium webdriver(Java)

I'm trying to test scrolling functionality by selenium webdriver. Same is working in Firefox but not in chrome driver. Here is basic code I'm using for scrolling.

Actions a = new Actions(driver);
WebElement el = driver.findElement(By.xpath("//*[@id='dsm-frame']"));
a.moveToElement(el).clickAndHold().moveByOffset(0, 1000000).release().perform();

Is there any specific reason that Action builder does not work in chrome? Kindly advise how it can be worked in Chrome driver.

Thanks

like image 368
Tanmay Avatar asked Jan 13 '16 05:01

Tanmay


People also ask

Why scroll down is not working in Selenium?

Selenium cannot directly scroll up or down with its methods. This is done with the Javascript Executor. DOM can interact with the elements with Javascript. Selenium runs the commands in Javascript with the execute_script() method.

How do I scroll down in Java using Selenium?

Selenium can execute JavaScript commands with the help of the executeScript method. To scroll down vertically in a page we have to use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.

How do I scroll down screen in Selenium?

Selenium runs the commands in Javascript with the execute_script() method. For scrolling down to the bottom of the page, we have to pass (0, document. body. scrollHeight) as parameters to the method scrollBy().


1 Answers

You can use JavaScriptExecutor for scrolling.

Scroll Down

((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scr‌​ollHeight);");

Scroll Up

((JavascriptExecutor)driver).executeScript("window.scrollTo(0,0);");
like image 138
Paras Avatar answered Sep 22 '22 19:09

Paras