Website Automation Using C # and WebBrowser

I create an application that opens a website in a WebBrowser control, and then puts some text in the fields, and then clicks a few buttons one by one.

Take a look at the code below ...

 var doc = webBrowser1.Document.GetElementById ("ddlOnBoro");
 doc.SetAttribute ("SelectedIndex", "3");
 var temp = doc.InvokeMember ("change");

 doc = doc.Document.GetElementById ("iddOnstreet_txTextBox");
 doc.SetAttribute ("value", "ASTOR PLACE");

 var adoc = doc.Document.GetElementById ("Button6");
 var getCrossStreets = adoc.DomElement as mshtml.HTMLInputButtonElement;
 adoc.RaiseEvent ("onclick");

The first and last 3 lines work fine, and even the middle 2 works fine, but when I'm in RaiseEvent ("onclick") in the last line of code, the value of the text field becomes empty before sending, even I set it in the 5th line of code.

The website is embedded in ASP.NET, and I think it is a ViewState that is messed up.

Any ideas?

+7
c # browser
source share
3 answers

http://watin.org/ is based on Watir and allows for unit testing of websites. It can also be used to enter form data, clear data from websites, etc. This is perfect for use with C #. Please do not use this for vile purposes.

If you are on a website in IE, will the text box clear when the button is clicked? If you try to reproduce what your program is trying to do manually, and the same will not happen, you may miss something. For example, in the first three lines you raise the "change" event (which, in my opinion, is really "onchange"), but you do not do this for the next two lines. Just by looking at it, that is the only difference between your code and its execution manually.

+3
source share

See the following article.

It provides a small example of how to use WebBrowser to automatically fill in Google search text. I will start with this, and then try to enter my own site with different field names.

C # Auto click button and autocomplete form

+2
source share

As a reference, check if the popular iMacros browser macro recorder can handle this website. If so, then you know, at least, that this is doable.

+1
source share

All Articles