Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException if using FindControl on a form having control w/o name attribute set #3

Open
fehdem opened this issue Jan 23, 2020 · 0 comments

Comments

@fehdem
Copy link

fehdem commented Jan 23, 2020

I used this little piece of code to get access to an TextInputControl on a form.

using (var browser = new MechanizeBrowser())
{
  var page = await browser.NavigateAsync("https://<blabla>");
  if (page.IsHtml)
  {
    var form = page.Forms["login"];
    var queryfield = form.FindControl<TextInputControl>("param3");
    ....
  }
}

The login form contains some controls, some w/o having the name attribute set. This results in a NullReferenceException when calling form.FindControl.
I cloned the repro locally and found the issue in HtmlForm.cs.

public T FindControl<T>(string Name) where T : HtmlFormControl
{
  return (T)this.FirstOrDefault(item => item.Name != null && item.Name.Equals(Name,StringComparison.OrdinalIgnoreCase));
}

The code on Master lacks a check whether item.Name is set to any string. I added a null check as a bugfix. If I don't miss any side effect you may include this into your master as well.

Best regards,
Marcus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant