how to convert Htmlnode of HtmlAgilityPack to webbrowser HtmlElement

c# html-agility-pack

Question

I was creating a application that automatically inserts data into html input tags. I have xPath for specific tag like '/html/body/form/div/div[2]/div/div/input' and I managed to get HtmlNode with the help of HtmlAgilityPack

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

Now i need to somehow select HtmlElement from Webbrowser.Document which corresponds to current HtmlNode . Can somebody help me with that?

BTW: I am not creating any spamming bot.

Hi everyone again. I found solution with recursion, lots of if statements and no htmlagilitypack, but unfortunately i can't post it right now. It seems that i don't have enough reputation.

Still, if it doesn't make too much effort, can you please tell me how to solve this problem with htmlagilitypack, because my code seems really nasty.

Popular Answer

Thank you everyone. After thinking and programming for a almost whole day, I came to decision that i have to use native htmlElement instead of htmlagilitypack HtmlNode, because I want to input text into Htmlelement in webbrowser . so here is the code that i came up with. still i would appreciate if someone shows a solution with 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;
    }

function is called in this way. where htmlController is instance of some class.

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why