Wednesday, February 15, 2006

Submitting a form while automating IE using vb.net

If you are planning to automate IE using VB.net and you have previously used WebBrowser control as an ActiveX control you could very well be surprised. You just might feel restricted using the System.Windows.Forms.WebBrowser control compared to the ActiveX Control. The first and the biggest problem you might face is that the System.Windows.Forms.WebBrowser is very strongly type checked, which makes things like in the example below impossible to achive.
AxWebBrowser1.Document.forms(0).q.value = "foxpose"
AxWebBrowser1.Document.Forms(0).submit()
Using the newer managed control you need to use the example given below.
WebBrowser1.Document.All("q").SetAttribute("value", "foxpose")
WebBrowser1.Document.Forms(0).InvokeMember("Submit")
This took quite a lot of my time yesterday. I hope this code somehow helps someone out when they are programming the new managed control in .NET 2.0 Incase you want a more detailed example just leave a comment and I will modify the post to include a more detailed example.


Technorati Tags: , ,

6 comments:

JSCOTTY said...

I think this is close to what i am looking for, any help would be appreciated! I need webbrowser control using vb6 to automatically select an option in a combo drop down on a webpage and actually click the object so that it submits input to script. let's say the name of object is mycombobox and the option i need selected is the value of 32, this is what i used
WebBrowser1.Document.All("mycombobox").Value = "32" which puts the correct text for option 32 in the text field of the combo box but it does not acutally submit it to the onchange event in the java script.

Francis said...

Thanks so much for the info. Helped me a lot!

Mark said...

Your post was very helpful. However, I wasn't able to submit the form using your WebBrowser1.Document.Forms(0).InvokeMember("Submit") statement. Instead I ended up clicking the submit button as follows: WebBrowser1.Document.All("submitbtn").Click()

Vasudeva said...

can you please look at the scenario when using AJAX controls, using update panel? When using "Submit", it just post backs the control does not do much in the screen...

Padiyath Deepak said...

Hi,
I tried to submit the form using both the way
(1.WebBrowser1.Document.Forms(0).InvokeMember("Submit") and 2.WebBrowser1.Document.All("submitbtn").Click())

I am not sure why this happen. I could retrieve the element but invokemember and click are not working. The element has the id "Submit" and it returns an object successfully. My code lokks sumthng like this

this.webBrowser1.Document.GetElementById("username").SetAttribute("value","***");
this.webBrowser1.Document.GetElementById("password").SetAttribute("value","***");
webBrowser1.Document.Forms[0].InvokeMember("Submit()"); --> The last line does not generate any result


Is thr a Solution....

Thanks,

Deepak Padiyath

Steve said...

Very helpful - thanks.
for Padiyath Deepak - if it still helps, I suspect form 0 is not the form with the button you are looking to submit. Try forms[1]