Reputation: 237
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
Upvotes: 8
Views: 34921
Reputation: 529
This is what worked for me.
Actions action = new Actions(driver);
action.sendKeys(Keys.PAGE_DOWN).perform();
Upvotes: 0
Reputation: 176
If you are trying to click the element after scrolling then try below snippet.
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Upvotes: 0
Reputation: 1
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...
Upvotes: 0
Reputation: 594
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
Upvotes: 0
Reputation: 81
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();
}
Upvotes: 0
Reputation: 348
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
Upvotes: 2
Reputation: 237
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.
Upvotes: 4
Reputation: 3235
You can use JavaScriptExecutor
for scrolling.
Scroll Down
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight);");
Scroll Up
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,0);");
Upvotes: 5