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.
Thanks for sharing this useful knowledge. Please how can I use it to scroll a google map page to collect reviews with python selenium? e.g https://www.google.com/maps/place/Cafe+1842/@43.453504,-80.5449067,13z/data=!4m10!1m2!2m1!1s+!3m6!1s0x882bf40ce0834e6d:0x39ecc3e2beaeaa5c!8m2!3d43.4669595!4d-80.5232248!9m1!1b1
I need to scroll down to see more reviews. Thanks
LikeLike
I am trying to scroll down to bottom to grafana url but its not working…could you please give some alternative way
LikeLike