7
votes

I am building an application which opens a website in WebBrowser control and then puts some text in fields and then clicks submits on few buttons one after another.

Have a look at code bellow...

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");

First and last 3 lines work good and even middle 2 works fine but when I RaiseEvent("onclick") in the last line of code, the value of textbox gets blank before being submitted even I've set it in 5th line of code.

The website is built into ASP.NET and I think this is ViewState that is messing up with.

Any ideas?

2
Why do you write such application?Ladislav Mrnka
Have you considered something like Watir? (watir.com)Graham

2 Answers

3
votes

http://watin.org/ is based off Watir and allows for unit testing of web sites. It can also be used for entering form data, scraping data from web sites, etc. It's perfect for use with C#. Please don't use this for nefarious purposes.

If you're on the web site in IE, will the textbox clear out when you click the button? If you try to replicate what your program is trying to do by hand and the same thing does not happen, then you may be missing something. For example, in the first three lines, you call the "change" event (which I think is really "onchange") but you don't do it for the next 2 lines. Just by looking at it, that's the only difference between your code and doing it by hand.

2
votes

Please see the following article.

It provides a small example on how to use the WebBrowser to auto fill in search text into Google. I would start with that and then try entering your own site with different field names.

C# Auto click button and auto fill form