user1196604 user1196604 - 15 days ago 4
Vb.net Question

Sending Keys to webbrowser vb.net?

I have a webbrowser in my vb.net application and I would like to enter text into a textbox on a site. When button 1 is clicked, it programmatically finds the text box and types the message in.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendKeys.Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}")
SendKeys.Send("The text I want to send to the control.")
End Sub

End Class


However, it doesn't work. The tab keys do get the curser in the right place but when the text is pasted in the application crashes. Whats gone wrong?

Answer

Since you are using a webbrowser control, you can access the element by name. For example, this will put text into Google's search box and then click the Google Search button:

WebBrowser1.Document.All("q").SetAttribute("Value", "Text value.")
WebBrowser1.Document.All("btnK").InvokeMember("click")