首页 文章

如何将HtmlAgilityPack的Htmlnode转换为webbrowser HtmlElement

提问于
浏览
2

我正在创建一个自动将数据插入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解决这个问题,因为我的代码看起来真的很讨厌 .

2 回答

  • 1

    谢谢大家 . 在思考和编程了将近一整天后,我决定使用原生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");
    
  • 0

    如果您知道元素的某个位置,您可以简单地获取元素

    HtmlNode mynode=htmlDocument.DocumentNode.SelectSingleNode("//div[@class='fooclass']");

    或者您可以使用HtmlNodeCollection的Select函数 .

    获取某个节点后,只需使用mynode变量Attributes,InnerHtml或InnerText属性即可 .

    例如:如果您的节点引用图像 mynode.Attributes["src"].Value 将显示图像源uri .

    PS:我假设htmlDocument是HtmlAgilityPack的类 .

相关问题