简体   繁体   English

从跨度读取值

[英]Read Value from span

i want to try a programm which reads out Values of a website with Geckofx. 我想尝试一个使用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: 我要访问的HTML代码:

<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. 48.066是我要读取的值。

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 :) 我现在搜寻了大约2天的解决方案,可以继续进行我的私人项目,希望有人可以帮助我:)

Solutions i tried: 我尝试的解决方案:

Test 1: 测试1:

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

Test 2: 测试2:

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

If testelement is null, you're not loading the page correctly, or the source is incorrect. 如果testelement为null,则说明您未正确加载页面,或者源不正确。

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: 并在webBrowser_DocumentCompleted中:

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

Most likely, you need to wait for the document to finish loading before looking for elements. 最有可能的是,您需要等待文档完成加载才能查找元素。

Browser.DocumentCompleted += (sender, e) => 
{
    var testElement = Browser.Document.GetElementById("class_test") as GeckoElement;
    // TODO: handle testElement being null
    string text = testElement.GetAttribute("value");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM