10

I'm not a QA person, just a front end developer. I'm using the Selenium IDE plugin for the Firefox browser to make some simple tests.

The site I'm making the tests for uses ajax to bring in new content via infinite scroll. So each time the user scrolls to the bottom of the page new content is loaded. How can I get Selenium IDE to scroll to the bottom of the page? What would the command look like for that?

I tried:

<tr>
    <td>keyPress</td>
    <td>xpath=/html/body/</td>
    <td>35</td>
</tr>

35 is the key value for the "end" key on the keyboard. When a human presses that on this site it brings the user to the bottom of the page and triggers the infinite scroll. But when selenium does it, the test passes but the screen doesn't indicate that key has been pressed. The page doesn't go to the bottom and the ajax isn't triggered.

Anyway ideas how to achieve this?

5 Answers 5

5

Selenium automatically calls the javascript scrollIntoView function on any element you try to interact with. If you know an element exists at the bottom of the page then doing anything with that element (including getting an attribute or hovering over it, etc) will cause the page to scroll.

Alternatively, you are probably on the right track with trying to scroll down with either End or Page Down key, however I'm most familiar with Selenium2/Webdriver so I can't speak to the correctness of your approach above.

3
  • Thanks @SamWoods. focus(locator) will do the trick with a locator known to be at the bottom of the page. Dec 4, 2013 at 11:49
  • focus didnt do the trick for me, isnt scrolling it, and keypresses have for some reason become disturbing for me, figuring out a js function might be too timely right now, dont want it to bloat the test, so i think im out of options so far until something easy and logical comes along for this. i just need to scroll back up to an element so that at the time when the test stores a var, the eyes can visually see the data at the same time. Jun 29, 2016 at 18:45
  • You should try using the action builder to hover over the element. Basic example for java would be: hover = Action.new(driver); hover.moveToElement(toElement); hover.perform;
    – Sam Woods
    Jun 29, 2016 at 18:59
4
# function to handle dynamic page content loading - using Selenium
def fb_scroll():
    # define initial page height for 'while' loop
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        new_height = driver.execute_script("return document.body.scrollHeight")

        if new_height == last_height:
            break
        else:
            last_height = new_height

That's what you need using python and selenium web driver. Hope this helps.

1
  • Worked nice for my setup. The only modification I did was to take into account the fact that the scrolling zone was not the entire webpage, but a single div in the page. So I used: driver.execute_script("return arguments[0].scrollHeight", scrollable_element) and driver.execute_script("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrollable_element) Jul 29, 2019 at 13:07
2

In case someone finds this useful this is what I use in Selenium IDE:

<tr>
<td>storeEval</td>
<td>window.scrollBy(0,500)</td>
<td></td>
</tr>

You can vary the vertical value ie 500, to go further down the page.. I believe the horizontal value ie 0 could also be useful..

1
  • Also works in SeleniumIDE by adding Command: "run script", target: "window.scrollBy(0,500)"
    – Dror
    Jul 23, 2019 at 16:24
1

Late solution but posted as a reference:

My project is also using Ajax in order to bring new content (down scrolling). After 20 posts there's a banner you can click on.

I used the code below to scroll down the 20 posts and then click on the banner:

Command: sendKeys
Target: "<The Xpath of the 20th post (the post just before the banner)>"

Then I used the command clickAndWait in order to click on the banner.

1
  • worked flawlessly, finally a good solution, for me atleast, scrolling up that is, see my comment above. good work. :-) i used sendKeys and in target put xpath=html/body/div[1]/section/div[2]/section[1] in my case, and it scrolled right up. BAM!!!! 97X, the future of Rock and Roll! Jun 29, 2016 at 18:49
0

I am not sure whether you could do that in IDE. But I do have a solution if you want to do that in WebDriver. There are two approaches to do so and you might like the one where you can execute some javascript . The first option is to use Keys class to simulate any keypress event in WebDriver. Regarding the other one, you can execute javascript (scrollTo) by casting the WebDriver instance to JavascriptExecutor.

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;


public class ScrollPage {
    public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.nytimes.com/");
     ((JavascriptExecutor)driver).executeScript("scrollTo(0,3000)");
        //  driver.findElement(By.id("shell")).sendKeys(Keys.END);
       Thread.sleep(5000L);
      ((JavascriptExecutor)driver).executeScript("scrollTo(0,3000)");
        }

    }

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.