【问题标题】:how to convert Htmlnode of HtmlAgilityPack to webbrowser HtmlElement如何将 HtmlAgilityPack 的 Htmlnode 转换为 webbrowser HtmlElement
【发布时间】:2012-06-13 17:25:12
【问题描述】:

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

【问题讨论】:

  • @MaziarBouali 嗨,我如何在将 Htmlnode 转换为字符串后选择特定的 htmlElement?

标签: c# html-agility-pack


【解决方案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");

【讨论】:

    【解决方案2】:

    如果你知道你元素的某个位置,你可以简单地通过

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

    或者您可以对 HtmlNodeCollection 使用 Select 函数。

    获取特定节点后,只需使用 mynode 变量 Attributes、InnerHtml 或 InnerText 属性即可。

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

    PS:我假设 htmlDocument 是 HtmlAgilityPack 的类。

    【讨论】:

    • 对不起,我没听懂。我将如何获得 webbrowser.Document 中的 HtmlElement?你能提供最简单的例子吗,即使它只是/html/body?
    • string html_str=webrowser.DocumentText; 描述为 msdn.microsoft.com/tr-tr/library/… 之后您可以在 HtmAgilityPack HtmlDocument 中加载 html_str,例如 htmldoc.LoadHtml(html_str);
    • 我希望这能满足您的需求 string html_srt = webBrowser1.DocumentText; HtmlAgilityPack.HtmlDocument html_doc = new HtmlDocument(); html_doc.LoadHtml(html_srt); html_doc.DocumentNode.SelectNodes("//div[@class='fooclass']//h2//a");
    • 我认为我们对我的问题有误解。我想要做的是,自动输入、选择、检查数据到 webbrowser html 标签中。例如有 并且在 webbrowser 加载之后,我想在其中输入一些文本。我尝试了上面的代码,它只选择节点,而不是实际的 html 元素,这意味着我不能用 webbrowser 做任何事情。我希望这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多