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

8

7

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

Hylomorphism answered 13/1, 2016 at 5:56 Comment(7)
try a.moveToElement(el).build().perform();Incuse
you may try sending space like el.sendKeys(Keys.SPACE);Closehauled
Try ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight);");Chamomile
I tried with a.moveToElement(el).build().perform() but it did not work. Why is that it is not working in Chrome but in Firefox only.Hylomorphism
Do you get any errors?Incuse
@guy , i did not get any error. Code got executed on Chrome but the scroll down action did not happen.Hylomorphism
Is it same to be a.moveByOffset(0, 1000000).release().perform(); ?Lonni
C
5

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);");
Chamomile answered 13/1, 2016 at 7:50 Comment(6)
Can it be not done without Javascript executor and only using Selenium APIs.Hylomorphism
you can try something like driver.findElement(By.id("")).sendKeys(Keys.PAGE_DOWN); for scroll downChamomile
I tried using sendKeys(Keys.PAGE_DOWN) but it throws error 'org.openqa.selenium.WebDriverException: unknown error: cannot focus element' in chrome.Hylomorphism
if these things aren't working. then you can go with JavaScriptExecutorChamomile
I tried using JavaScriptExecutor as said above to scroll down but it is throwing exception ** org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: missing )**Hylomorphism
There was some weight character in the script when I copied it from this answer, so try to copy this one which has worked for me: window.scrollTo(0,document.body.scrollHeight);Disaccord
H
4

So far below are my findings based on options I tired:

1.Action builder class : Works in Firefox but not in Chrome.Not sure why it did not work in Chrome.

2.js.executeScript("window.scrollTo(0, document.body.scrollHeight);");: It neither worked in Firefox and Chrome. I guess this is something not suitable in my case.

3.js.executeScript("arguments[0].scrollIntoView(true);",element); : It worked in both Firefox and Chrome.

Hylomorphism answered 14/1, 2016 at 5:35 Comment(0)
T
2

Javascript Executer version (scrolls to bottom - best for my needs):

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight);");

Webdrvier only version:

driver.findElement(By.id("INSERT_A_INPUT_BOX")).sendKeys(Keys.PAGE_DOWN);

This version only scrolls down one page. At first it wouldn't work for me because I was trying to focus on a random element not an input element, but it does work when focusing on something you can type into

Tribade answered 13/1, 2016 at 20:12 Comment(0)
T
0

I had a similar problem in Selenium (wrapped in Selenide):

This worked for me to scroll to a link that was off the bottom of the page:

if (isPhantomjs()){
    $(byText(linkType)).scrollTo().click();
} else {
    executeJavaScript("arguments[0].scrollIntoView(true);", $(byText(linkType)));
    $(byText(linkType)).click();
}
Teodoor answered 13/10, 2016 at 20:28 Comment(0)
A
0

For Scroll down:

WebDriver driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

OR,also

jse.executeScript("scroll(0, 250);");

For Scroll up:

jse.executeScript("window.scrollBy(0,-250)", "");

OR,

jse.executeScript("scroll(0, -250);");

Hope it will help you

Afoot answered 16/10, 2016 at 13:33 Comment(0)
C
0

I am not a specialist at all of java but I use chromedriver from vba and face the same problem. As I am not able to locate the webelements by XPATH / JSON, I use the source code of the page instead, senkeys tab until the webelement I search get the focus then this element becomes the activeelement so the scrollintoview on the element is accessible and I can refresh the source code of the page...

Cedillo answered 25/12, 2020 at 19:16 Comment(0)
S
0

If you are trying to click the element after scrolling then try below snippet.

Actions action = new Actions(driver);

action.moveToElement(element).click().perform();

Sabella answered 27/12, 2020 at 16:52 Comment(0)
D
0

This is what worked for me.

Actions action = new Actions(driver);
action.sendKeys(Keys.PAGE_DOWN).perform();
Discrete answered 27/11, 2023 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.