• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Scroll Up Using Selenium in Python

Scroll Up Using Selenium in Python

May 20, 2022 Leave a Comment

To scroll up using Selenium in Python, you can use the Selenium webdriver execute_script() function which executes JavaScript code in the browser.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Top of Webpage
driver.execute_script("window.scrollTo(0,0)")

The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browser.

When working with web browsers, it’s possible you may want to scroll up to the top of a webpage or scroll up to a particular pixel height.

To scroll up using Selenium in your Python code, you can use the Selenium webdriver execute_script() function. execute_script() is a versatile function and allows you to execute JavaScript code in the browser.

With JavaScript, we can easily scroll up in a browser.

How to Scroll Up to Top of Webpage Using Selenium

To scroll up to the top of a webpage using Selenium, you can use the execute_script() function to execute the JavaScript function window.scrollTo() and pass ‘0’ for the second parameter.

Below shows you a simple example of how you can use Selenium to scroll up to the top of a webpage.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Top of Webpage
driver.execute_script("window.scrollTo(0,0)")

How to Scroll Up to Specific Height Using Selenium

If you want to scroll up to a specific pixel height, then you can change the second parameter of the window.scrollTo() function.

For example, if you want to scroll up to pixel 100, then pass ‘100’ to window.scrollTo().

Below shows you how to use Selenium to scroll up to a specific height in your Python code.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Pixel Height 100
driver.execute_script("window.scrollTo(100))

Hopefully this article has been useful for you to learn how to scroll up using Selenium in Python.

Other Articles You'll Also Like:

  • 1.  Convert Integer to Bytes in Python
  • 2.  How to Check if List is Empty in Python
  • 3.  How to Subtract Two Numbers in Python
  • 4.  Calculate Sum of Dictionary Values in Python
  • 5.  Changing Python Turtle Speed with speed() Function
  • 6.  Using Python to Repeat Characters in String
  • 7.  Python sin – Find Sine of Number in Radians Using math.sin()
  • 8.  Using Python to Sort Two Lists Together
  • 9.  Using Python to Split String by Newline
  • 10.  Using Python to Sum Even Numbers in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy