I've been diving into the world of selenium and Java, attempting to utilize JavaScript for scrolling down to a specific element. However, I've encountered an issue where my script scrolls past the element, rendering it out of view. The error message displayed in the console reads:
Exception in thread "main" org.openqa.selenium.ScriptTimeoutException: script timeout
I'm puzzled as to why this is happening. Can anyone shed some light on what might be causing this problem?
public class JavaScriptScrolling {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.asda.com/");
Thread.sleep(5000);
driver.findElement(By.xpath("//button[@id='onetrust-accept-btn-handler']")).click();
Thread.sleep(3000);
JavaScriptUtils jUtil = new JavaScriptUtils(driver);
WebElement element = driver.findElement(By.xpath("//a[text()='Explore more']"));
jUtil.scrollIntoView(element);
element.click();
}
}
The scrollIntoView method from the JavaScriptUtils class used for scrolling is as follows:
public void scrollIntoView(WebElement element) {
js.executeAsyncScript("arguments[0].scrollIntoView(true);", element);
}