7

I tried using webBrowser1.Document.Body.ScrollTop and webBrowser1.Document.Body.ScrollLeft, but they don't work. They always return 0 and I can't access webBrowser1.Document.documentElement.ScrollTop and .ScrollLeft.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Niek H.
  • 585
  • 1
  • 5
  • 19

7 Answers7

14

OK, I solved it:

Dim htmlDoc As HtmlDocument = wb.Document
Dim scrollTop As Integer = htmlDoc.GetElementsByTagName("HTML")(0).ScrollTop
Leif Wickland
  • 3,693
  • 26
  • 43
Marc
  • 13,011
  • 11
  • 78
  • 98
  • I have searched the vast web high and low and viewed myriad solutions which have not worked, but this, finally this, gave me what I wanted. Thank you, Marc. – Leif Wickland Sep 29 '11 at 05:01
  • This does not work!!!, Method name expected?? plz tell me what i'm missing i to have searched the web high and low for 3 days :'( – Pomster Jun 20 '12 at 13:23
  • This answer is written in VB. – William Jockusch Feb 27 '16 at 11:50
  • Also, there is System.Windows.Forms.WebBrowser and System.Windows.Controls.WebBrowser. Finding code online can be very confusing if you are working with the other WebBrowser class. – William Jockusch Feb 28 '16 at 09:14
  • 1
    I put the C# equivalent of Marc's VB code in an answer below. It works :) – kurt Apr 22 '16 at 22:50
5

To actually scroll, we found that the ScrollIntoView method worked nicely. For example, to scroll to the top-left of the page.

 this.webBrowser.Document.Body.FirstChild.ScrollIntoView(true);

However, we were not successful in actually getting the scroll position (that said, we didn't spend long trying). If you are in control of the HTML content, you might consider using some javascript to copy the scroll position into a hidden element and then read that value out using the DOM.

ScrollTop and ScrollLeft merely allow an offset to be provided between the boundary of an element and its content. There appears to be no way to manipulate the scroll by those values. Instead, you have to use ScrollIntoView.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • I'm not in control of the HTML content so that won't work for me. I do know about the ScrollIntoView and ScrollTo functions, but instead of scrolling i want to access the scrollbar in the webbrowser component to read out the values. I could of course insert some javascript in the page myself and let it read out the values, but that's an option i rather not use. – Niek H. Aug 14 '09 at 13:38
3

For anyone interested, here's the C# code equivalent to Marc's answer:

System.Windows.Forms.HtmlDocument htmlDoc = webBrowser.Document;
if (htmlDoc != null)
{
    int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
    int scrollLeft = htmlDoc.GetElementsByTagName("HTML")[0].ScrollLeft;
}
kurt
  • 584
  • 13
  • 16
2

I was able to query the scroll position using this

        if (this.webBrowser.Document != null)
        {
            int scrollPosition = this webBrowser.Document.Body.ScrollTop;                
        }
thowa
  • 590
  • 1
  • 8
  • 29
0

You can check documentElement

IHTMLElement2 page = 
(wb.Document.DomDocument as IHTMLDocument3).documentElement as IHTMLElement2;
int pos = page.scrollTop;
Ahmad
  • 8,811
  • 11
  • 76
  • 141
0

Accepted answer is VB. For C# WPF WebBrowser, I had to write the following. No idea if I really need all those casts or not. If one can get rid of any of those casts, that would be terrific.

using mshtml;
int? GetScrollTop(System.Windows.Controls.WebBrowser browser)
{
  object doc = browser.Document;
  HTMLDocument castDoc = doc as HTMLDocument;
  IHTMLElementCollection elements = castDoc?.getElementsByTagName("HTML");
  IEnumerator enumerator = elements?.GetEnumerator();
  enumerator?.MoveNext();
  var first = enumerator?.Current;
  IHTMLElement2 castFirst = first as IHTMLElement2;
  int? top = castFirst?.scrollTop;
  return top;
}
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
0

I found kurt's answer almost worked but had to change the array reference as follows:

var document = (HTMLDocument)Browser.Document;
var scrollTop = (int)document.getElementsByTagName("HTML").item(0).ScrollTop;
Mike Ward
  • 3,211
  • 1
  • 29
  • 42