Search code examples
c#geckogeckofx

Get website's html to a textbox


I was trying this but keep getting the error that gecko doesn't contain a definition for innerHTML..

GeckoElement g2element = null;
g2element = (GeckoElement)mainbrowsersrc.Document.GetElementByTagName("html");
rich1.Text = g2element.InnerHtml; // 48.066

or

  rich1.Text = mainbrowsersrc.Document.GetElementsByTagName("html").innerHtml;

Solution

  • If you need the HTML of the entire page, then you should go with

    (mainbrowsersrc.Document.DocumentElement as Gecko.DOM.GeckoHtmlHtmlElement)?.InnerHtml;
    

    Please notice that the error that you get is because there is no method .GetElementByTagName(name); - the method is called GetElementsByTagName(name) - plural form.

    This is because the tg name is not unique and the method returns a collection of elements with the same tag name - for example a collection of li (list item) elements.

    Consequently, if you want to get a particular element by tag name, you should do something like:

    string html = mainbrowsersrc.Document.GetElementsByTagName("html").FirstOrDefault().innerHtml;
    //or 
    html = mainbrowsersrc.Document.GetElementsByTagName("html")[0].innerHtml;