0

I am running this script to test my websites' login handling capabilites:

WebBrowser wb = new WebBrowser();
string url = "mywebsites url";
wb.Url = new Uri(url);
wb.Document.GetElementById("username").SetAttribute("value", userName[0]);

Ive done a simmilar thing for the password and the submit. So the problem is that i get a warning saying NullReferenceException was unhandeled on this line:

wb.Document.GetElementById("username").SetAttribute("value", userName[0]);

despite having the url defined with the line above that one. Is there another reason other then the URL that i might have a nullreference error?

And how do i solve this? is there a better way of making a bot to test login capabilities of a web app?

Dimitar
  • 1,148
  • 8
  • 29
  • 1
    I believe you are supposed to call wb.Navigate(new Uri(url)); before the Document property will be set. – Pieter Nov 15 '18 at 21:10
  • If I understand correctly what it is you are trying to do, then something like Selenium might be very helpful. Take a look at this post: https://stackoverflow.com/questions/6334065/how-do-i-use-selenium-in-c – David Tansey Nov 15 '18 at 21:30

1 Answers1

1

If you're getting a Null reference error on that line, then either:

wb.Document.GetElementById("username") is null (ie the element doesn't exist on the page)

or:

userName[0] is null (userName object is null or has no elements).

You should put in a breakpoint and inspect these things.

Jonathan
  • 4,916
  • 2
  • 20
  • 37
  • Yeah, it turns out i had an error with populating the array, which i didnt notice. I solved it nevertheless. Thank you for your answer. – Dimitar Nov 16 '18 at 17:45