TESTEROPS

A pragmatic approach to QA and OPS

Scrolling Down a Page

A very common use of the web page is to browse through the contents of a webpage. And it involves scrolling up and down a webpage, until you have reached the bottom of your webpage.

In this post, we’re going to see, how we can scroll down a webpage, to the bottom.


import unittest
from selenium import webdriver
class fblogin(unittest.TestCase):
      def setUp(self):
          self.driver=webdriver.Firefox()
          self.driver.get("http://www.facebook.com")
          self.driver.maximize_window()


      def test_facebookLogin(self):
          driver=self.driver
          driver.find_element_by_id('email').click()
          driver.find_element_by_id('email').clear()
          driver.find_element_by_id('email').send_keys('facebook id')

          driver.find_element_by_id('pass').click()
          driver.find_element_by_id('pass').clear()
          driver.find_element_by_id('pass').send_keys('password')


          driver.find_element_by_xpath("//input[@type='submit']").click()
          driver.implicitly_wait(30)


          #using the JavaScriptExecutor to scroll down to bottom of window
          driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


      def tearDown(self):
          self.driver.quit()
if __name__ == "__main__":
unittest.main()

As per the documentation mentioned here :

The window object in DOM has a scrollTo method to scroll to any position of an opened window. The scrollHeight is a common property for all elements. The document.body.scrollHeight will give the height of the entire body of the page.

2 thoughts on “Scrolling Down a Page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: