1

I am trying to send user to the bottom of page

by javascript

I am trying this code

window.scrollTo(0,document.body.scrollHeight);

it does not work

but if i try

window.scrollTo(0,9999999999999999);

it works very well

Is page height can be greater than 9999999999999999 ?

Or there is better way?

Alaa Gamal
  • 1,135
  • 7
  • 18
  • 28

3 Answers3

4

Try this:

document.body.scrollTop = document.body.scrollHeight;
document.documentElement.scrollTop = document.documentElement.scrollHeight;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    @AlaaGamal—use both, e.g. `window.scrollTo(0, document.documentElement.scrollTop || document.body.scrollTop)`. Most browsers want the first, some the second. – RobG Oct 09 '12 at 03:39
  • A few years ago `document.body.scrollTop = document.body.scrollHeight;` was working at Google Chrome, now only `document.documentElement.scrollTop = document.documentElement.scrollHeight;` works. – Konard Apr 11 '19 at 20:06
2

For Scroll down in Selenium use below code:

JavascriptExecutor jse = (JavascriptExecutor) driver;  
// (driver is your browser webdriver object)
jse.executeScript("window.scrollBy(0,document.body.scrollHeight || document.documentElement.scrollHeight)", "");

For scroll up use below code:

jse.executeScript("window.scrollBy(0,-document.body.scrollHeight || -document.documentElement.scrollHeight)", "");
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Ankit Gupta
  • 776
  • 5
  • 12
1

You can always send them to Number.MAX_VALUE, that is guaranteed to be the biggest possible.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106