将密钥发送到网页浏览器 vb.net?

发布于 2025-01-07 18:36:22 字数 517 浏览 6 评论 0原文

我的 vb.net 应用程序中有一个网络浏览器,我想在网站的文本框中输入文本。单击按钮 1 时,它会以编程方式查找文本框并在其中键入消息。

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

但是,它不起作用。 Tab 键确实使光标位于正确的位置,但是当将文本粘贴到应用程序中时,应用程序崩溃了。出什么问题了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稍尽春風 2025-01-14 18:36:22

要将密钥发送到 WebBrowser,请先获取 WebBrowser 焦点,然后再获取 SendKeys。使用以下代码:

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

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
梅倚清风 2025-01-14 18:36:22

由于您使用的是网络浏览器控件,因此您可以通过名称访问该元素。例如,这会将文本放入 Google 的搜索框中,然后单击 Google 搜索按钮:

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

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文