Jose Bernhardt Jose Bernhardt - 3 months ago 27
Java Question

Scroll to specific element Selenium WebDriver Java

Im using selenium (Java) and chrome driver to run few scripts on a website. I want to scroll the driver or the page to an specific element located on the page. It might be visible. I understand that with a JavaScripExecutor Its possible, but so far the way Im doing it I can only scroll certain "spaces".
Here is what I have:

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

Answer

If you want to scroll to specific element, you should try using scrollIntoView(true) as below :-

//Find that specific element first 
WebElement element = driver.findElement(..);

//Now scroll to this element 
jse.executeScript("arguments[0].scrollIntoView(true);", element);