user1196604 user1196604 - 1 year ago 140 Question

Sending Keys to webbrowser

I have a webbrowser in my 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("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 Source

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.")