so i have a loop in vb.net that loads a webpage, fills out a form and clicks submit
I currently am using these for, respectively, waiting for the webpage to load, filling out the form, and clicking submit
Do While Not browser.ReadyState = WebBrowserReadyState.Complete
System.Windows.Forms.Application.DoEvents()
Loop
and
browser.Document.GetElementById("text").SetAttribute("value", message)
and
For Each element As HtmlElement In browser.Document.GetElementsByTagName("input")
If element.GetAttribute("type") = "submit" Then
element.In开发者_如何学JAVAvokeMember("click")
End If
Next
but my problem is that after around the fifth time the loop is run it srrors on the line with the set attribute. And i have a feeling that it is not waiting for the webpage to load before it tries filling out the form, and that is why it is erroring.
Does anyone know a better way to do this?
You can use DocumentCompleted Event, this event ensures your document is ready, and all required sections loaded
Private Sub browser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browser.DocumentCompleted
' YOUR FORM FILLING CODE HERE
End Sub
and for form submit you can use, forms' submit() method like this
browser.Document.GetElementById("text").DOMElement.form.submit()
Maybe the element "text" isn't loaded yet or isn't on the page?
You could make some javascript to do that task, and inject it into the page.
Here is how to make a "click" in javascript: https://developer.mozilla.org/en/DOM/document.createEvent
精彩评论