c# – 如何通过GetElementByClass选择一个类并以编程方式单击它

我一直在尝试使用此代码在html / ajax中按类读取元素,因为知道GetElementByClass不是webBrowser.Document中的选项.我似乎无法获得返回值然后调用该成员.有没有解决这个问题?

参考文献:
Getting HTMLElements by Class Name

例:

<span class="example">(<a href="http://www.test.com/folder/remote/api?=test" onclick=" return do_ajax('popup_fodder', 'remote/api?=test', 1, 1, 0, 0); return false; " class="example">test</a>)</span>

示例代码:

   HtmlElementCollection theElementCollection = default(HtmlElementCollection);
   theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
   foreach (HtmlElement curElement in theElementCollection)
   {
        //If curElement.GetAttribute("class").ToString = "example"  It doesn't work.  
        // This should be the work around.
        if (curElement.OuterHtml.Contains("example"))
        {
            MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire.
            // InvokeMember(test) after class is found.
        }
    }
我承认它不是很直观,但你需要使用GetAttribute(“className”)而不是GetAttribute(“class”)

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
    if (curElement.GetAttribute("className").ToString() == "example")
    {
        MessageBox.Show(curElement.GetAttribute("InnerText")); // Do something you want
    }
}
https://stackoverflow.com/questions/3436116/how-to-select-a-class-by-getelementbyclass-and-click-on-it-programmically

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c# – 如何通过GetElementByClass选择一个类并以编程方式单击它