How to Automate Scrolling#
Browserist provides a simple way to scroll a web page βΒ both up and down, to specific elements or sideways. This can be useful when you need to interact with elements that are not immediately visible or when you want to take screenshots of content that is only loaded when you scroll.
Note
Similar to scrolling on a touch screen or mouse, you can only scroll down if you're not already at the bottom of the page, and you can only scroll up if you're not already at the top of the page. Or you can't scroll up exactly 50 pixels if you're already 20 pixels from the top of the page β instead you scroll up 20 pixels. Keep this in mind when trying to replicate the examples below.
Scrolling Up and Down#
By Disstance#
Pixels#
How to scroll down by a number of pixels, relative to the current position:
How to scroll up by a number of pixels, relative to the current position:
Pages#
How to scroll a page down, relative to the current position:
How to scroll a page up, relative to the current position:
It's also possible to scroll up or down by a number of pages:
Example with Mixed Methods#
Let's imagine that we want to take screenshots of a page that requires scrolling to load all the content. We can scroll down by a page and then a little up to capture screenshot partials of the entire page:
Python | |
---|---|
To Specific Element#
How to scroll down or up to make a specific element visible in the viewport:
To Absolute Position#
How to scroll to the end of the page:
How to scroll to the top of the page:
How to scroll to an absolute position on the page with x
and y
coordinates, where y=100
is the vertical position:
Or simply:
Scrolling Sideways Left and Right#
By Disstance#
Pixels#
How to scroll right by a number of pixels, relative to the current position:
How to scroll left by a number of pixels, relative to the current position:
To Absolute Position#
How to scroll to an absolute position on the page with x
and y
coordinates, where x=100
is the horizontal position:
Or simply:
Example#
Let's imagine that we want to ensure that we can't scroll sideways on a page. We try to scroll a little right and then a little left to ensure that the page is locked in place:
Conditional Scrolling#
How to Check If Scrolling Is Possible#
You can check if you're at the end or top of the page with these methods:
Example#
Example where we want take a screenshot of the footer of a page:
Python | |
---|---|
Only Scroll If Necessary#
Instead of checking whether an element is visible in the viewport before scrolling to it...
... such logic is already combined in this method:
Example#
Example in context where we want to make sure an element is visible in the viewport before taking a screenshot of it: