Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Website Automation Using C# and WebBrowser

Tags:

browser

c#

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?

like image 944
Umair A. Avatar asked Jul 31 '10 14:07

Umair A.


People also ask

Can you automate with C?

C is one of the fastest, most compatible languages that exist and as a result it is a perfect candidate for automation. Some of the examples can be: Self driving cars: Self driving cars need to perform A LOT of computation REALLY FAST.

Does Selenium use C#?

Selenium Web driver is mostly used with Java and C#.

Can you use C# for automation?

C# is useful for automation testing because it allows the automation test engineer to develop an application with the help of Visual Studio on the . Net framework. C# is another programming language that also supports the binding with Selenium. And this language binding will be updated along with the java program.

Can we use C++ for Selenium?

You can use selenium server and JsonWireProtocol. In C++ you can implement CURL requests to selenium server and do web automation with C++. Use this link first: My fork of Webdriver++. There are also some C++ libraries that do this work.


2 Answers

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.

like image 82
Derreck Dean Avatar answered Sep 18 '22 13:09

Derreck Dean


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

like image 45
jzacharuk Avatar answered Sep 19 '22 13:09

jzacharuk