Search code examples
c#htmlgetelementbyidgeckofx

Read Value from span


i want to try a programm which reads out Values of a website with Geckofx. Now i have the following Problem that i dont get the needed Values and it shows to me it is null.

The HTML Code i want to access:

<li id="box" class="tooltip" title="">
                <div class="classname"></div>
                <span class="value">
                    <span id="class_test" class="">48.066</span>
                </span>
</li>

48.066 is the Value i want to read.

I searched now for about 2 days for a solution that i can go on with my private project i hope anyone can help me :)

Solutions i tried:

Test 1:

GeckoElement testelement = null;
testelement = (GeckoElement)Browser.Document.GetElementById("class_test");
string text = testelement.GetAttribute("value");

Test 2:

GeckoHtmlElement testelement = null;
            testelement = (GeckoHtmlElement)Browser.Document.GetHtmlElementById("class_test");
            string text = testelement.InnerHtml;

Solution

  • If testelement is null, you're not loading the page correctly, or the source is incorrect.

    This works fine:

    string content = "<html><body><li id=\"box\" class=\"tooltip\" title=\"\">"+
        "<div class=\"classname\"></div>" + 
        "<span class=\"value\">" + 
        "<span id=\"class_test\" class=\"\">48.066</span>" + 
        "</span></li></body></html>";
    webBrowser1.LoadHtml(content, "http://www.example.com");
    

    And in webBrowser_DocumentCompleted:

    GeckoElement testelement = null;
    testelement = (GeckoElement)webBrowser1.Document.GetElementById("class_test");
    string text = testelement.InnerHtml; // 48.066