views:

295

answers:

2

I have a WebBrowser control where I want to edit a field in the HTML and then submit a form. Let's say that the field is called txtUname and that the submit button is called submit in form form1. How can I do this?

I'm currently thinking of using something like this, but I haven't tested it:

//Change value

webBrowser1.Document.getElementbyId("txtUname").setAttribute("value","this is my value");

//Submit form

webBrowser1.Document.Forms("form1").invokeMember("submit");

In addition, does this work for any type of field?

Also, is it possible to do something similar inside a WebClient?

+1  A: 

It may help you to look at the extended version of the Webbrowser control found here and here. It shows how to get the document and the elements including fields. A webclient, on the other hand is lower level then using webBrowser1 object in respect to your code sample. Using WebClient works at the http level and it can be used at the ftp level also. Hope this helps. Best regards, Tom.

tommieb75
+1  A: 

It would be much more difficult to do with a WebClient, if even possible.

Why are you trying to do it though? Because if it is to test the web pages then you are better off with Selenium or WaitN.

They will allow you to do something like this in a unit test:

[Test] 
public void SearchForWatiNOnGoogle()
{
 using (IE ie = new IE("http://www.google.com"))
 {
  ie.TextField(Find.ByName("q")).TypeText("WatiN");
  ie.Button(Find.ByName("btnG")).Click();

  Assert.IsTrue(ie.ContainsText("WatiN"));
 }
}

That example is from the WaitN homepage...

Glenn Condron