Scroll Element into View with Selenium

Have tried many things with respect to scroll, but the below code has provided better results. This will scroll until the element is in view: WebElement element = driver.findElement(By.id(“id_of_element”)); ((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true);”, element); Thread.sleep(500); //do anything you want with the element

Scroll Automatically to the Bottom of the Page

jQuery isn’t necessary. Most of the top results I got from a Google search gave me this answer: window.scrollTo(0, document.body.scrollHeight); Where you have nested elements, the document might not scroll. In this case, you need to target the element that scrolls and use it’s scroll height instead. window.scrollTo(0, document.querySelector(“.scrollingContainer”).scrollHeight); You can tie that to the … Read more

Maintain/Save/Restore scroll position when returning to a ListView

Try this: // save index and top position int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null) ? 0 : (v.getTop() – mList.getPaddingTop()); // … // restore index and position mList.setSelectionFromTop(index, top); Explanation: ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, … Read more

Smooth scrolling when clicking an anchor link

Update April 2018: There’s now a native way to do this: document.querySelectorAll(‘a[href^=”#”]’).forEach(anchor => { anchor.addEventListener(‘click’, function (e) { e.preventDefault(); document.querySelector(this.getAttribute(‘href’)).scrollIntoView({ behavior: ‘smooth’ }); }); }); This is currently only supported in the most bleeding edge browsers. For older browser support, you can use this jQuery technique: $(document).on(‘click’, ‘a[href^=”#”]’, function (event) { event.preventDefault(); $(‘html, body’).animate({ scrollTop: … Read more