【问题标题】:HTMLElement's GetAttribute("html") or GetAttribute("text") not returning a valueHTMLElement 的 GetAttribute("html") 或 GetAttribute("text") 不返回值
【发布时间】:2012-11-12 07:33:57
【问题描述】:

使用此示例,我如何使用 C# HTMLElement 的 Get Attribute 在此行上获取“GOOGLE CLICK”。假设这个元素是使用 getElementByTagName 吸收的。我不确定要使用哪种属性我尝试过“html”和“text”(例如element.GetAttribute("html")) 属性适用于 java,但遗憾的是不适用于 c#。

<a href="www.google.com">GOOGLE CLICK</a>

这里是我尝试运行的 c# 示例代码供参考。

//web is the current page i am at.
        HtmlElementCollection links = web.Document.GetElementsByTagName("a");
        foreach (HtmlElement link in links)
        {
            if (link.GetAttribute("text") == "GoogleClick")
                MessageBox.Show(this, "Hooray I got it!");
        }

【问题讨论】:

    标签: c# getelementsbytagname getattribute getelementsbyname


    【解决方案1】:

    “Google Click”不是属性,而是内部文本。

    HtmlElementCollection links = web.Document.GetElementsByTagName("a");
    foreach (HtmlElement link in links)
    {
        if (link.InnerText == "GoogleClick")
            MessageBox.Show(this, "Hooray I got it!");
    }
    

    【讨论】: