Skip to content

Commit 4f3f1aa

Browse files
authored
Merge pull request #1220 from RedZone908/form_documentation_enhancements
Form documentation enhancements
2 parents 7fa4d5f + d0298d6 commit 4f3f1aa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

docs/tutorials/04-Forms.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ if (queryInput != null)
3030

3131
The latter version is much more verbose and requires additional checking on our side. The upside of this version is that we can also treat the error case (i.e., the expected field with the name `q` is not found or is not an `IHtmlInputElement`), which is completely ignored in the former version.
3232

33+
Some webpages will not handle the form submission correctly unless it is triggered from a specific control on the page. This can be done like:
34+
35+
``` cs
36+
// first research how to identify the submit element on the page you're working with, then...
37+
IHtmlInputElement submitButton = queryDocument.QuerySelection<IHtmlInputElement>("#submitbutton");
38+
IDocument resultDocument = await form.SubmitAsync(submitButton);
39+
```
40+
Note that the above scenario requires setting the form values via the more verbose mechanism.
41+
3342
An important aspect of submitting forms is that we require at least a default loader. Forms usually require making an HTTP request for loading a new document (mostly using the POST verb), which yields the demand for having a requester configured upfront. Note that sometimes forms even come with stronger requirements, especially if they are properly secured. In such cases the use of `WithCookies()` to configure a cookie container could be mandatory.
3443

3544
The code for using a cookie container looks almost identical:

docs/tutorials/06-Questions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ var input = document.QuerySelector<IHtmlInputElement>("input[type=file][name=myI
188188
input?.Files.Add(file);
189189
```
190190

191+
Depending on the webpage, you may also need to set `input.Value` to the name of the file, or alternatively set it in `SubmitAsync()`'s overload that accepts the form values. If your file-based form submission results in a `System.NullReferenceException` from `HtmlFormElement.SubmitAsync()`, that indicates that `input.Value` is what is missing.
192+
191193
In the previously used example the file variable refers to any IFile instance. AngleSharp is a PCL does not come with a proper implementation out of the box, however, a simple one may look like:
192194

193195
```cs

0 commit comments

Comments
 (0)