1

Form:

<form method="POST" name="contactform" action="form-handler.php"> 

    <label for='name'>Name:</label>

    <input type="text" name="name" />

    <label for='email'>Contact Number:</label>

    <input type="text" name="phone" /> 

    <label for='phone'>Email:</label>

    <input type="text" name="email" /> 

    <label for='message'>Requirements:</label> 

    <textarea name="message"></textarea>

    <input type="submit" value="Submit" name="submit" class="quoteButton" />

</form>

Code:

        HtmlNode.ElementsFlags.Remove("form");
        HtmlNodeCollection fromNodes = doc.DocumentNode.SelectNodes("//form");
        foreach (HtmlNode formNode in fromNodes)
        {
            var inputs = formNode.SelectNodes(".//input");
        }

"//input" works and when I check the children xpath I see:

/html[1]/body[1]/div[1]/div[3]/div[2]/div[1]/div[1]/input[2]

Which means, according to HAP, form doesn't even include the input!

".//input" that selects subnodes of the current formNode doesn't work (returns null)!

How to fix this I added the following but it doesn't work

 HtmlNode.ElementsFlags.Remove("form"); 

Any idea?

Edit (+Example):

In the next sample the inputs variable is also null.

        var doc = new HtmlDocument();
        doc.LoadHtml(@"
            <!DOCTYPE html>
            <html>
                <head>
                    <title>Form Test</title>
                </head>
                <body>
                    <form>
                        <input type=""text"" />
                        <input type=""reset"" />
                        <input type=""submit"" />
                    </form>
                </body>
            </html>
            ");

        HtmlNode.ElementsFlags.Remove("form");
        IEnumerable<HtmlNode> fromNodes = doc.DocumentNode.Descendants("form");
        foreach (HtmlNode formNode in fromNodes)
        {
            var inputs = formNode.SelectNodes(".//input");
        }
Nizar Blond
  • 1,826
  • 5
  • 20
  • 42
  • have you tried `/input` ! – Anirudha Feb 06 '14 at 10:25
  • yes, it doesn't help, the problem is that HAP doesn't recognize input a child of Form. – Nizar Blond Feb 06 '14 at 10:26
  • 1
    A few questions: 1) do you need to use the `form` in the first place? `//input[@class="quoteButton"]` isn't enough for what you want to do? 2) Where did you get the code sample you showed us? If you got it from your browser you might to check HAP is indeed dealing with the exact same code. 3) Can you select the relevant `form` itself? Did you check it was in the `formNodes`? – Robin Feb 06 '14 at 11:10
  • 1)Yes, sometimes I might have more than one form, that's not enough for me. 2) doc.OuterHtml includes the correct form as above. formNode includes "
    ". That's what I said, it is not parsing the form correctly, input isn't considered part of form when chosen.
    – Nizar Blond Feb 06 '14 at 11:48
  • Are you sure that `inputs` is null? It should be an empty (but non-null) collection if no matching nodes are found. Are you sure you're looking at `inputs` after that line has executed (and before the end of the `foreach` loop)? You're not actually using the `inputs` variable inside the loop. Are you somehow accessing it outside the loop? – JLRishe Feb 06 '14 at 13:36
  • yes i am sure, using a debugger. you can check it yourself. – Nizar Blond Feb 06 '14 at 13:52
  • Duplicate of: http://stackoverflow.com/questions/2385840/how-to-get-all-input-elements-in-a-form-with-htmlagilitypack-without-getting-a-n – PHPGuru Feb 17 '17 at 19:10

1 Answers1

2

Solved!

        HtmlNode.ElementsFlags.Remove("form");

should be called before loading the document.

My bad :D

Nizar Blond
  • 1,826
  • 5
  • 20
  • 42