public static boolean scrollUntilVisible(WebDriver driver, WebElement element, int maxScrollAttempts) {
int scrollAttempts = 0;
int scrollIncrement = 100; // The amount of increment to use for each swipe
int targetY = element.getLocation().getY(); // y- koordinaat for element
while (scrollAttempts < maxScrollAttempts) {
if (element.isDisplayed()) {
return true; // Return true if the element is visible
}
// Scroll down the page
((JavascriptExecutor) driver).executeScript("window.scrollBy(0, " + scrollIncrement + ");");
try {
Thread.sleep(500); // wait
} catch (InterruptedException e) {
e.printStackTrace();
}
scrollAttempts++;
}
return false; // Return false when certain number of tries is reached
} //return false when certain number of tries is reached
}
New contributor
Reyhan Yıldız is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.