0

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)");
            Thread.sleep(2000);
            executor.executeScript("window.scrollTo(0, 0)");
            Thread.sleep(2000);
        }
        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) {
        e.printStackTrace();
    }
    }

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) {
            exception.printStackTrace();
        }
    }
1
  • 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 Sep 30, 2022 at 13:40

2 Answers 2

0

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.

Approach:

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)
0

It's possible that the scrolling function is failing because of the sleep time used. Since the page may load at different speeds, it's best to wait for the page to fully load before attempting to scroll to the bottom, top or to an element.

One solution is to use explicit waits to wait for the elements to load before scrolling. For example, you can wait for the presence of an element at the bottom of the page before scrolling to the bottom. Here's an example code snippet:

public void scrollToBottom() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//footer")));
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }
    catch(Exception e) {
        test.log(Status.FAIL, e);
    }
}

In this example, we're using an explicit wait to wait for the footer element to be present on the page before scrolling to the bottom. You can similarly wait for other elements to be present before scrolling.

Also, you can use the Actions class in Selenium to scroll to an element, which may be more reliable than using JavascriptExecutor. Here's an example code snippet:

public void scrollToElement(WebElement element) {
    try {
        Actions actions = new Actions(driver);
        actions.moveToElement(element);
        actions.perform();
    } catch (Exception exception) {
        test.log(Status.FAIL, exception);
    }
}

This code uses the Actions class to move to the specified element, which will scroll the page if necessary. This may be a more reliable way to scroll to an element on the page.

References:

Your Answer

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

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