日期:2014-05-18  浏览次数:20784 次

如何用WebBrowser控件获取下面的HtmlElement
如何获取下面红色字体的<a>元素
<html>
<ul>
<li calss="a1"><a><span>a1</span></a></li>
<li calss="a2"><a><span>a2</span></a></li>
<li calss="a3"><a><span>a3</span></a></li>
<li calss="a4"><a><span>a4</span></a></li>
<li calss="a5"><a><span>a5</span></a></li>
</ul>
</html>

------解决方案--------------------
C# code
private void button1_Click(object sender, EventArgs e)
{
  webBrowser1.DocumentText = @"<html>
<ul>
<li calss=""a1""><a><span>a1</span></a></li>
<li calss=""a2""><a><span>a2</span></a></li>
<li calss=""a3""><a><span>a3</span></a></li>
<li calss=""a4""><a><span>a4</span></a></li>
<li calss=""a5""><a><span>a5</span></a></li>
</ul>
</html>";
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  HtmlElementCollection li = webBrowser1.Document.GetElementsByTagName("li");
  for (int i = 0; i < li.Count;i++ )
  {
    if (li[i].GetAttribute("calss") == "a3")
    {
      HtmlElement link = li[i].FirstChild;
      MessageBox.Show(link.InnerHtml);
    }
    }
}