C#webbrowser applition,如何在TextArea中单击超链接

时间:2013-06-19 07:55:15

标签: c# hyperlink browser textarea

html代码可能看起来像

<html>
<body>
......
<textarea>
    <div>
        <a href="http://www.sohu.com">This is a hyper link </a>
    </div>
</textarea>
......
</body>
</html>

当我通过

枚举链接时
        foreach (HtmlElement item in ieBrowser.document.Links)
        {
            var innerText = item.InnerText;
            if ((string.IsNullOrEmpty(innerText)) || innerText.Length == 0) continue;
            if (innerText.Contains(task.ShopName))
            {
                item.ScrollIntoView(true);
                item.Focus();
                item.SetAttribute("selected", "true");
                item.SetAttribute("target", "_self");
                item.InvokeMember("Click");     
                break;
            }
        }

但是,似乎webbrowser无法识别textArea中的链接。 任何知道如何解决这个问题的人。

1 个答案:

答案 0 :(得分:0)

可以使用InnerHtml属性提取textarea上的内容。然后,您可以使用HTMLAgilityPack查找链接并进行导航

HtmlElement textArea = webBrowser1.Document.All["textareaid"];
if (textArea != null)
{
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.Load(textArea.InnerText);
    foreach(HtmlAgilityPack.HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
    {
       HtmlAgilityPack.HtmlAttribute att = link["href"];
       webBrowser1.Navigate(att.Value);
    }
}