Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium wrong scroll into view

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?

like image 872
ctina Avatar asked Feb 03 '26 22:02

ctina


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);
like image 79
Florent B. Avatar answered Feb 05 '26 13:02

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!