user1196604
user1196604

Reputation: 105

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?

Upvotes: 1

Views: 12676

Answers (2)

mzurita
mzurita

Reputation: 690

To send keys to WebBrowser, get WebBrowser focus and later SendKeys. Use the following code:

 Me.WebBrowser1.Document.Body.Focus()
 System.Windows.Forms.SendKeys.Send("...") 'Whatever keys combination you want

Upvotes: 2

John Koerner
John Koerner

Reputation: 38077

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

Upvotes: 0

Related Questions