The JS method for scrolling is scrollTo. To make Selenium scroll down to the very bottom of the page, you need to use the webpage’s height (document.body.scrollHeight) – no need to use its absolute value:
2 – Scrolling to the top of the page
As you may have expected, this is done using the same method, but what we want to do is scroll the negative height of the webpage:
3 – Scrolling to a specific element
To achieve this, you must first identify the web element using the correct locator. Let’s say it’s stored in a variable called “myElement”. What our code will do is to scroll until myElement is visible, using the scrollIntoView method:
Just like before, the first thing we have to do is find the element. After that, clicking on it is quite easy:
This one can be particularly useful when we try to avoid Selenium exceptions such as elementNotInteractableException or ElementClickInterceptedException.
5 – Refreshing the browser
Basically this tells the browser to go back 0 pages.
6 – Going back X number of pages
Yeap, just replace the 0 from the previous line of code with the number of Back actions you want to perform:
This line of code with take you back 3 pages.
7 – Testing sessionStorage