2013-06-19 50 views
0

的HTML代码可以当我通过C#的WebBrowser applition,如何单击超链接内文本区域

 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; 
      } 
     } 

枚举的样子

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

的链接,但,似乎web浏览器无法识别textArea内的链接。任何知道如何处理这个问题的人。

回答

0

textarea上的内容可以使用InnerHtml属性提取。然后你可以使用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); 
    } 
} 
+0

thaks.i知道它。但是我想是'的HtmlElement item.InvokeMember(“点击”);''没有的“href”'属性。 –

+0

不可能,textarea的内容不会被解析为DOM – volody

相关问题