Question Web Form Filling

sheenitmathew

Member
Joined
Feb 4, 2013
Messages
9
Programming Experience
5-10
Dears

I want to develop a desktop application to fill the fields in a web form. I could do this using the .NET frame work 3, but when the platform is changed to 4.0, then it generates error. I cannot change the framework since I use some function that does not exist in 3 (such as String.IsNullOrWhiteSpace). My code follows.


Private Sub frmWebBrowser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
WebBrowser1.Navigate("mylink.....")
End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted


Dim element As HtmlElement = WebBrowser1.Document.GetElementById("forwardbtn2")
If Not element Is Nothing Then
element.All("forwardbtn2").InvokeMember("click") 'I want to click a button programatically. This works fine in frame work 3, but generates error in framework 4: Object reference not set to an instance of an object.

Else
MessageBox.Show("Check your internet connection", "Erroe", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub

Please help me solve this issue. I also want to use the GetAttribute() and SetAttribute() functions to get and set values on the web form. Thanks in advance for your help.
 
I wrote applications that do very much the same, and I've got no problem using .InvokeMember("click") until now.
Then, again, you must be sure of where your Html Element is.
If you set the variable element with WebBrowser1.Document.GetElementById("forwardbtn2"), it seems that "element" is already your aim.
So, it seems, you should state element.InvokeMember("click") and not element.All("forwardbtn2").InvokeMember("click"), because the latter will get the element "forwardbtn2", and then will look for a child of it whose Id is also "forwardbtn2". When it is not found - because there is not any - it will return Nothing and generate the exception you reported, because you can't command .InvokeMember over Nothing.
I hope it helps. Good Luck!
 
In case you accept an extra piece of advice, whenever you know exactly which HTML element you want do deal with, in large and intrincate documents, it is a good thing to give a two-step specification with Tag and Id/Name.
For instance:
Dim NameInputHtmlElement = WebBrowser1.Document.GetElementsByTagName("input").Item("name")
Or even more specific:
Dim NameInputHtmlElement = WebBrowser1.Document.GetElementsByTagName("div").Item("personal_info").GetElementsByTagName("input").Item("name")
Then you can go further:
NameInputHtmlElement.SetAttribute("value","John Doe")
or get more sophisticated:
If NameInputHtmlElement.GetAttribute("value")<>"John Doe" then NameInputHtmlElement.SetAttribute("value","John Doe")
and
 NameInputHtmlElement.RaiseEvent("onblur")
and the already known
 NameInputHtmlElement.InvokeMember("click")

I think WebBrowser control itself is a little bit limited, but its automation via VB.NET is a very interesting issue. It can get pretty complex depending on the websites you deal with.
Good Luck!
 
Back
Top