How to scroll Page in Selenium Python

In this post, we will learn how to scroll Page in Selenium python for example. The Selenium web driver API is used for test automation on Windows or mobile, It helps us remotely control the different browsers automatically as if a real user is interacting by stimulating activities like opening or closing a new window, or tab, entering text in the textbox, select values from the dropdown, mouse control.

1. How to scroll Page in Selenium Python


In this example, we will learn how to scroll Page in selenium Python First, we will load a chrome browser driver with the help of ChromeDriverManager that automatically handles install of a new chrome driver , whenever the new version of the driver is released. To scroll the page in selenium python, First, we have to use the driver.get(“https://news.google.com/”) method to navigate to the link that is given as a parameter. it will open the google news web page and find the element by tag name text “div”.

  • driver.get(): Navigate to a new link that is given as parameter
  • execute_script() : This method is used to execute a javascript method window.scrollTo(0, Y)
  • sleep(3): wait 3 seconds until it scroll down.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

driver.get("https://news.google.com/") 

print("The First opened window  = " + driver.title)

driver.execute_script("window.scrollTo(0, Y)")
time.sleep(3)
driver.close()
driver.refresh()
  

2. How to scroll Page in selenium Python


In this example, we have to use the driver.get(“https://news.google.com/”) method to navigate to the link that is given as a parameter. The execute_script() is used to execute the javascript method window.scrollTo(0, document.body.scrollHeight). The document.body.scrollHeight return the height of an element including padding. So it will return the height of the page and scroll vertically down

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

driver.get("https://news.google.com/") 

print("The First opened window  = " + driver.title)


driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

time.sleep(3)
driver.close()
driver.refresh()

3. How to scroll Page in selenium Python vertically


In this example, we have to use the driver.get(“https://news.google.com/”) method to navigate to the link that is given as a parameter. The execute_script() is used to execute the javascript method window.scrollTo(0, document.body.scrollHeight) to scroll the page vertically with extra 600px

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

driver.get("https://news.google.com/") 

print("The First opened window  = " + driver.title)


driver.execute_script("window.scrollTo(0, window.scrollY + 600)")
time.sleep(3)
driver.close()
driver.refresh()

4. How to scroll Page in selenium Python using div


The python selenium driver.get(“https://news.google.com/”) method to navigate to the link that is given as a parameter. The driver.maximize_window() is used to maximize the open web page. To scroll using the element ‘div’ vertically First, we have found the element by tag name using the method ‘find_element_by_tag_name() of the selenium library and iterating through all ‘div’ on the page. The execute_script() is used to execute the javascript method window.scrollTo(0, document.body.scrollHeight) to scroll the page

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager



driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

driver.get("https://news.google.com/") 

while driver.find_element_by_tag_name('div'):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    Divs=driver.find_element_by_tag_name('div').text
    if 'End of Results' in Divs:
        print('end')
        break
    else:
        continue


driver.close()

Summary

In this post, we have learned How to scroll Page in Selenium Python