Selenium wrong scroll into view

Viewed 1070

If some buttons are visible or not, my program is doing different actions. In order to see those buttons i need to scroll to them.

I want to see this: enter image description here

instead i see like this: enter image description here

I use this code:

JavascriptExecutor je=(JavascriptExecutor) driver;
     WebElement elem=driver.findElement(By.xpath("//a[contains(.,'"+username+"')]/ancestor::div[contains(@class, 'js-answer-element')]"));
     je.executeScript("arguments[0].scrollIntoView(true)", elem);

How can scroll until all the square is into view?

1 Answers

The container is scrolled at the top of the view, which is exactly what you instructed by calling arguments[0].scrollIntoView(true).

If you want it at the bottom instead:

je.executeScript("arguments[0].scrollIntoView(false)", elem);

Or with an offset from the top:

je.executeScript("arguments[0].scrollIntoView(true); window.scrollBy(0, -60);", elem);
Related