Authorization using AngleSharp

458 Views Asked by At

I am trying to sign in Habr.com with AngleSharp. I can fill e-mail and password fields, but don't know how to submit the form. If you know any other ways, could you share them, please?

string pathPageLogin = "https://account.habr.com/login/?state=43fb94b4773d9f8940cc47fd59b79b5c&consumer=habr&hl=ru_RU";

var config = Configuration.Default.WithDefaultLoader().WithDefaultCookies();
IBrowsingContext browsingContext = BrowsingContext.New(config);
await browsingContext.OpenAsync(pathPageLogin);
browsingContext.Active.QuerySelector<IHtmlInputElement>("#email_field").Value = "***";
browsingContext.Active.QuerySelector<IHtmlInputElement>("#password_field").Value = "***";
browsingContext.Active.QuerySelector<IHtmlInputElement>("#login_form > fieldset > div.form__buttons.s-buttons > button").DoClick();

html:

 <div class="form__field s-field">
            <label for="email_field" class="form__field-label">E-mail</label>
            <input type="email" class="form__field-input" value="" name="email" data-required="true"  id="email_field" data-validate_url="/ajax/validate/email/" />
          </div>

          <div class="form__field s-field">
            <label for="password_field" class="form__field-label">Пароль</label>
            <input type="password" class="form__field-input" value="" data-required="true" name="password" id="password_field" />
          </div>
...
<div class="form__buttons s-buttons">
            <button type="submit" name="go" class="button button_wide button_primary">
              Войти
            </button>
1

There are 1 best solutions below

0
Xerillio On

According to the documentation you can submit the form with SubmitAsync on the form element:

var form = queryDocument.QuerySelector<IHtmlFormElement>("#login_form");
var resultDocument = await form.SubmitAsync();