VBA 将 Enter 键发送到 HTMLInputElement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18853342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Sending the Enter key to an HTMLInputElement
提问by MattB
I have what I hope will be a simple question. I have a bit of code where I load a web page and enter a number into a field in IE. It is as follows:
我希望是一个简单的问题。我有一些代码,用于加载网页并在 IE 中的字段中输入一个数字。如下:
Option Explicit
Private Sub GettingCashAvail()
Dim IE As InternetExplorerMedium
Dim HTMLDoc As HTMLDocument
Dim IEField As HTMLInputElement
Set IE = New InternetExplorerMedium
IE.navigate "https://website.com/login.aspx"
While IE.Busy
DoEvents
Wend
Do Until IE.statusText = "Done"
DoEvents
Loop
Set HTMLDoc = IE.document
Set IEField = HTMLDoc.getElementById("ucGetAccountNumber_txtAccountNumber")
IEField.Value = "#########"
Now this field supports pressing enter to move to the next page. I could dim a second object for the submit button and then do something like IESubmit.Click to enter the information and navigate to the new screen, but what I'd like to do is somehow send the return key. Here is the portion of the source from the web page that I'm interested in:
现在该字段支持按回车键移动到下一页。我可以将提交按钮的第二个对象变暗,然后执行 IESubmit.Click 之类的操作以输入信息并导航到新屏幕,但我想做的是以某种方式发送返回键。这是我感兴趣的网页来源的一部分:
onkeypress="return clickButton(event,'ucGetAccountNumber_btnSubmit')"
Do you think there is any way to simply send the enter key? Thanks for the ideas if so, if not I can just get that submit button set to a second input object.
您认为有什么方法可以简单地发送回车键吗?如果是这样,感谢您的想法,如果不是,我可以将该提交按钮设置为第二个输入对象。
回答by David Zemens
You can maybe use:
你也许可以使用:
IE.Visible = True
Application.SendKeys "~" '## send the "Enter" keystroke
But in my attempts to use SendKeys
method, they're not very reliable, and most people will suggest using different methods.
但是在我尝试使用SendKeys
方法时,它们不是很可靠,大多数人会建议使用不同的方法。
I think personally it is preferable to create another object variable and invoke the button's .Click
method, as you indicated.
我个人认为最好创建另一个对象变量并调用按钮的.Click
方法,如您所指出的。