With same code I have tried scrolling, firstly I scrolled to bottom then top and then to element so that element can get loaded properly even though sometimes testcases are failing due to scrolling

public void pageScroller() {
        try {
            JavascriptExecutor executor = (JavascriptExecutor)driver;
            executor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
            executor.executeScript("window.scrollTo(0, 0)");
        catch(Exception e) {
            test.log(Status.FAIL, e);

//Tried below two functions for scrolling

```private void scrollToElement(WebElement articleSection2) {
    // TODO Auto-generated method stub
    try {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", getWebElement(articleSectionStr));
    } catch (Exception e) {

public void scrollToSection(WebElement element) {
        try {
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            int loc = element.getLocation().y;
            executor.executeScript("window.scrollTo(0, " + (loc - 150) + ");");
        } catch (Exception exception) {
  • 1
    Welcome to the community Ishmeet. Will be able to help you if you can share exact error or problem, your system, targeted platform, version etc Thanks Commented Sep 30, 2022 at 13:40

1 Answer 1


Automation scripts generally ran into these sort of issues where script fails when element is present at the bottom of the page and doesn’t get located. As a part software testing solutions and experience in automation, we are following the below mentioned approach to avoid these kind of failures.


Instead of scrolling page up and down directly you can initially wait for the page to load successfully and then you can scroll the window and locate the element accordingly. This, approach worked pretty well for us and resolved the issue with the locating elements after scrolling the window.

Here the code snippet in python: [We wait for the page to load successfully and then scroll the entire window to locate the element.]

def page_has_loaded(self):
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'

Once, the web page is loaded successfully and returned in its complete state we can scroll the entire window where our web elements resides and be able to locate easily. You can follow this code snippet to scroll the window.

    def scroll_window(self, by_locator):          
          self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight)", by_locator)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.