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,045
  • 6
  • 18
  • 27

3 Answers3

4

Try this:

document.body.scrollTop = document.body.scrollHeight;
document.documentElement.scrollTop = document.documentElement.scrollHeight;
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
  • 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
1

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

Jakub Hampl
  • 38,520
  • 9
  • 73
  • 103
1

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
  • 133,361
  • 24
  • 165
  • 234
Ankit Gupta
  • 737
  • 5
  • 12