如何將HtmlAgilityPack的Htmlnode轉換為webbrowser HtmlElement

c# html-agility-pack

我正在創建一個自動將數據插入html輸入標籤的應用程序。我有特定標籤的xPath,如'/ html / body / form / div / div [2] / div / div / input',我設法在HtmlAgilityPack的幫助下得到HtmlNode

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解決這個問題,因為我的代碼看起來真的很討厭。

熱門答案

謝謝大家。在思考和編程了將近一整天之後,我決定使用原生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是某個類的實例。

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



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因