2013-06-19 49 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

相關問題