With same code I have tried scrolling, firstly I scrolled to bottom then top and then to element so that element can get loaded properly even though sometimes testcases are failing due to scrolling
public void pageScroller() {
try {
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
Thread.sleep(2000);
executor.executeScript(“window.scrollTo(0, 0)”);
Thread.sleep(2000);
}
catch(Exception e) {
test.log(Status.FAIL, e);
}
}“`
//Tried below two functions for scrolling
“`private void scrollToElement(WebElement articleSection2) {
// TODO Auto-generated method stub
try {
((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView();”, getWebElement(articleSectionStr));
} catch (Exception e) {
e.printStackTrace();
}
}
public void scrollToSection(WebElement element) {
try {
JavascriptExecutor executor = (JavascriptExecutor) driver;
int loc = element.getLocation().y;
executor.executeScript(“window.scrollTo(0, ” + (loc – 150) + “);”);
} catch (Exception exception) {
exception.printStackTrace();
}
}
Source: Read More