AngleSharp Submit a Form

Viewed 978

in the example below

var url = "http://anglesharp.azurewebsites.net/PostUrlencodeNormal";
var config = new Configuration { AllowRequests = true };
var html = DocumentBuilder.Html(new Uri(url), config);
var form = html.Forms[0] as IHtmlFormElement;
var name = form.Elements["Name"] as IHtmlInputElement;
var number = form.Elements["Number"] as IHtmlInputElement;
var active = form.Elements["IsActive"] as IHtmlInputElement;
name.Value = "Test";
number.Value = "1";
active.IsChecked = true;
form.Submit();

And yet, it complains that Value is not an extension of IHtmlInputElement, use SetValue instead

but SetValues is basically a dictionary and I have no ideas how to use that with AngleSharp.

In another example, https://anglesharp.github.io/docs/Forms.html, it is also using

var queryInput = form.Elements["q"] as IHtmlInputElement;

if (queryInput != null)
{
   queryInput.Value = "anglesharp";
}

I assume after I set queryInput to a value, I can do

 var input = queryInput.Value to ensure the value is set.

The problem I have is I am setting three fields, namely

userID, pin, password before I hit submit. If I use AngleSharp, it bring me back to the same page which means that the authentication is not valid. I know the fields are filled properly with the value, I am just not sure it is actually passing those values over.

Thanks.

1 Answers

I would love to help you, but I fear we have potentially several issues here.

  1. Submitting the form with all input fields / the approach so far is correct, but could also be shortened a bit (AngleSharp has some helpers here)
  2. Potentially, JavaScript is in your way. If that's the case AngleSharp.Js may be helpful. Keep in mind though, that AngleSharp.Js is experimental and may not work for complex scripts / pages.

If you run into the latter then you have essentially two options: Either you'll use a bridge to a native (headless) browser (e.g., Chrome) instead, or you invest some time what the JS on the page is actually doing - after all you only care about the HTTP transfer(s). If you boil it down to the HTTP transfers you can use both - AngleSharp for page handling / cookies etc. and manual HTTP transfers for the rest.

Let me know if you need more infos - I can edit this response to fit better to your needs.

Related