11

.body.scrollHeight does not work in Firefox.

See: http://jsfiddle.net/gjrowe/X63KR/

What is the correct syntax to use instead?

G-J
  • 1,080
  • 2
  • 16
  • 32
  • These pages seems to contain info about the issue; http://www.webdeveloper.com/forum/showthread.php?73161-.scrollHeight-in-firefox, https://developer.mozilla.org/en-US/docs/DOM/element.scrollHeight, http://stackoverflow.com/questions/4369990/scrollheight-property-in-firefox –  Apr 10 '13 at 17:38
  • I got 0 on the alert but after adding a bunch of `
    ` it seems to work
    – Rodrigo Siqueira Apr 10 '13 at 17:42
  • @Allendar Other people asking the same with no answer – G-J Apr 10 '13 at 17:43
  • @RodrigoAssis Ok... I added some `
    ` and it seems to increase from 0 but it is not the height of the scrollable area... it seems to be the height of the content.
    – G-J Apr 10 '13 at 17:44
  • Sorry G-J. Try to see this jQuery hack; http://stackoverflow.com/questions/9471286/window-scrollfunction-not-working-on-firefox. You could backtrace the solution to the jQuery source and clone the functionality of the given hack/answer. –  Apr 10 '13 at 17:51

2 Answers2

21

This question has the same root problem as the thread at... Dynamically define iframe height based on window size (NOT CONTENT)

Understanding the issue at that thread will give the solution to this.

Basically, instead of using .body.scrollHeight, add this code...

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

The answer was obtained from: http://james.padolsey.com/javascript/get-document-height-cross-browser/

Community
  • 1
  • 1
G-J
  • 1,080
  • 2
  • 16
  • 32
  • Has this code stopped working in the past 3 years since the answer was given? I just tested it in Firefox and found that it yields the same value as just using document.body.scrollHeight. – Brandon Elliott Jul 03 '16 at 18:33
  • @BrandonElliott I'll test it and get back to you, but the issue is caused by body.scrollHeight not being reliable. – RSinohara Nov 15 '16 at 17:39
1

Use below code:

JavascriptExecutor jse = (JavascriptExecutor) (WebDriverObject);
jse.executeScript("window.scrollBy(0,document.body.scrollHeight || document.documentElement.scrollHeight)", "");
skirtle
  • 27,868
  • 4
  • 42
  • 57
Ankit Gupta
  • 776
  • 5
  • 12