Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks @SamWoods. focus(locator) will do the trick with a locator known to be at the bottom of the page. – AlessMascherpa Dec 4 '13 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. – Brian Thomas Jun 29 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 at 18:59

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.

share|improve this answer
    
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! – Brian Thomas Jun 29 at 18:49

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)");
        }

    }
share|improve this answer
# 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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