populating fields in webBrowser

M

M R

i am writing an application (C#) that automates the process of loading a web
page, login into a site and then simulates the navigation of the site.
the program uses a winform with a webBrowser control that is used to load
the web page. i am able to successfully navigate the site but am having
trouble entering data into the fields. the two fields are the username and
password. i would like to automatically enter the username and password into
the fields and then click the login button,.
the question is how to i programmatically access the controls on this
page in the webBrowser control and then click on the button?
thanks for any tips and hints
MR
 
M

Matt Lacey

i am writing an application (C#) that automates the process of loading a web
page, login into a site and then simulates the navigation of the site.
the program uses a winform with a webBrowser control that is used to load
the web page. i am able to successfully navigate the site but am having
trouble entering data into the fields. the two fields are the username and
password. i would like to automatically enter the username and password into
the fields and then click the login button,.
the question is how to i programmatically access the controls on this
page in the webBrowser control and then click on the button?
thanks for any tips and hints
MR

Try something like this:

/*Assuming the following HTML in document
*
* <form>
* <input type="text" name="username" />
* <input type="password" name="password" />
* <input type="submit" name="login" value="Log In" />
* </form>
*/

mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as
mshtml.IHTMLDocument2;

mshtml.HTMLInputElement otxtUserNameBox = (mshtml.HTMLInputElement)
doc.all.item("username", 0);
otxtUserNameBox.value = "test_uname";

mshtml.HTMLInputElement otxtPassWordBox = (mshtml.HTMLInputElement)
doc.all.item("password", 0);
otxtPassWordBox.value = "test_pword";

mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)
doc.all.item("login", 0);
btnLogin.click();
 
N

Nicholas Paldino [.NET/C# MVP]

MR,

You can use the Document property of the WebBrowser control to gain
access to the HTML DOM (this was pointed out already).

However, I would ask why not use the HttpWebRequest/HttpWebResponse
classes? A visual component isn't necessary for automating the process, as
you seem to have all the information you need. You could download the
content, parse it, and then send new requests based on that content.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top