2012-06-10 66 views
2

我正在創建一個自動將數據插入html輸入標籤的應用程序。 我的XPath像特定的標籤 '/ HTML /體/表格/ DIV/DIV [2]/DIV/DIV /輸入',我設法HtmlNode與HtmlAgilityPack如何將HtmlAgilityPack的Htmlnode轉換爲webbrowser HtmlElement

的幫助
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)webBrowser.Document.DomDocument; 
StringReader sr = new StringReader(documentAsIHtmlDocument3.documentElement.outerHTML); 
htmlDocument.Load(sr); 
    if (htmlDocument.DocumentNode != null) 
    { 
     HtmlNode currentNode = htmlDocument.DocumentNode.SelectSingleNode(xPath); 
    } 

現在我需要以某種方式從Webbrowser.Document中選擇與當前HtmlNode相對應的HtmlElement。有人可以幫助我嗎?

順便說一句:我沒有創建任何垃圾郵件機器人。

大家好了。我發現遞歸解決方案,很多if語句和沒有htmlagilitypack,但不幸的是我現在不能發佈它。看來我沒有足夠的聲望。但是,如果它沒有太多的努力,你能告訴我如何用htmlagilitypack解決這個問題,因爲我的代碼看起來真的很討厭。

+0

通話的ToString() –

+0

@MaziarBouali嗨,我如何可以選擇轉換Htmlnode串後具體HTML元素? –

回答

0

如果你知道你的元素的某些位置,你可以簡單地通過

HtmlNode MYNODE = htmlDocument.DocumentNode.SelectSingleNode( 「// DIV [@類= 'fooclass']」)獲得元素;

或者您可以使用HtmlNodeCollection的Select函數。

得到某個節點後,只需使用mynode變量Attributes,InnerHtml或InnerText屬性來滿足您的需求。

例如:如果您的節點引用圖像mynode.Attributes [「src」]。值將向您顯示圖像源uri。

PS:我認爲htmlDocument是HtmlAgilityPack的類。

+0

對不起,我沒有得到它。我將如何獲得webbrowser.Document內部的HtmlElement?你能提供一個最簡單的例子,即使它只是/ html/body嗎? –

+1

** string html_str = webrowser.DocumentText; **如上所述http://msdn.microsoft.com/tr-tr/library/system.windows.forms.webbrowser.documenttext.aspx當您可以在HtmAgilityPack HtmlDocument中加載html_str之後** htmldoc.LoadHtml(html_str); ** –

+0

我希望這涵蓋您的需要字符串html_srt = webBrowser1.DocumentText; HtmlAgilityPack.HtmlDocument html_doc = new HtmlDocument(); html_doc.LoadHtml(html_srt); html_doc.DocumentNode.SelectNodes( 「// DIV [@類= 'fooclass'] // // H2一個」); –

1

謝謝大家。經過幾乎整天的思考和編程之後,我決定必須使用native htmlElement而不是htmlagilitypack HtmlNode,因爲我想在webbrowser的Htmlelement中輸入文本。所以這裏是我提出的代碼。如果有人用htmlagilitypack顯示解決方案,我仍然很感激。

public HtmlElement selectHtmlNode(string xPath, HtmlElement htmlElement) 
    { 
     string currentNode; 
     int indexOfElement; 

     //get string representation of current Tag. 
     if (xPath.Substring(1,xPath.Length-2).Contains('/')) 
      currentNode = xPath.Substring(1, xPath.IndexOf('/', 1) - 1); 
     else 
      currentNode = xPath.Substring(1, xPath.Length-1); 
     //gets the depth of current xPath 
     int numOfOccurence = Regex.Matches(xPath, "/").Count; 

     //gets the children's index 
     int.TryParse(Regex.Match(currentNode, @"\d+").Value, out indexOfElement); 

     //if i have to select nth-child ex: /tr[4] 
     if (indexOfElement > 1) 
     { 
      currentNode = currentNode.Substring(0, xPath.IndexOf('[') - 1); 
      //the tag that i want to get 
      if (numOfOccurence == 1 || numOfOccurence == 0) 
      { 
       return htmlElement.Children[indexOfElement - 1]; 
      } 
      //still has some children tags 
      if (numOfOccurence > 1) 
      { 
       int i = 1; 
       //select nth-child 
       foreach (HtmlElement tempElement in htmlElement.Children) 
       { 
        if (tempElement.TagName.ToLower() == currentNode && i == indexOfElement) 
        { 
         return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement); 
        } 
        else if (tempElement.TagName.ToLower() == currentNode && i < indexOfElement) 
        { 
         i++; 
        } 
       } 
      } 
     } 
     else 
     { 
      if (numOfOccurence == 1 || numOfOccurence == 0) 
      { 
       return htmlElement.FirstChild; 
      } 
      if (numOfOccurence > 1) 
      { 
       foreach (HtmlElement tempElement in htmlElement.Children) 
       { 
        if (tempElement.TagName.ToLower() == currentNode) 
        { 
         return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement); 
        } 
       } 
      } 
     } 
     return null; 
    } 

函數以這種方式被調用。其中htmlController是某個類的實例。

HtmlElement currentElement = htmlController.selectHtmlNode("/body/form/div/div[2]/div/div/input", webBrowser.Document.GetElementsByTagName("html")[0]); 
currentElement.SetAttribute("Value", "hello world"); 
相關問題