简体   繁体   English

自动 Internet Explorer 时,Sendkey ("{ENTER}") 不起作用

[英]Sendkey ("{ENTER}") not working when automating Internet explorer

Sendkey ("{ENTER}") not entering data into website from excel Sendkey ("{ENTER}")未将数据从 excel 输入网站

I need to use it since form does not have a button to click我需要使用它,因为表单没有可以单击的按钮

Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://reference.medscape.com/drug-interactionchecker"
IE.Visible = True
While IE.Busy
DoEvents
Wend

Set TrackID = IE.Document.getelementbyid("MDICtextbox")
TrackID.Value = "Aspirin"
IE.Visible = True
SendKeys ("{ENTER}") 

End Sub

Note that, if I change enter to other command such as {BS} or Backspace it works but not enter请注意,如果我将 enter 更改为{BS}或 Backspace 等其他命令,它可以工作但不能输入

Did you try ~ (tilde) as this page suggest it :您是否尝试过〜(波浪号) ,正如本页所建议的那样:

SendKeys 发送密钥

As a rule of thumb, If you are using SendKeys , you are doing something wrong.根据经验,如果您使用的是SendKeys ,那么您做错了什么。

SendKeys is an extremely fragile function, and there are usually other "programmatic" methods SendKeys是一个极其脆弱的函数,通常还有其他“程序化”的方法

My Advice: Use the Page's javascript我的建议:使用页面的 javascript

I analyzed http://reference.medscape.com/drug-interactionchecker for you,我为你分析了http://reference.medscape.com/drug-interactionchecker

It turns out that the handleKeyDown function is bound to the onkeydown event,原来handleKeyDown函数绑定了onkeydown事件,

You can simply simulate the {ENTER} key by calling it directly您可以通过直接调用它来简单地模拟{ENTER}

handleKeyDown({which:13,preventDefault:function(){}});

(You can verify that it works by typing it in your browser's console) (您可以通过在浏览器的控制台中键入它来验证它是否有效)

You could call the function from vba with:您可以使用以下命令从 vba 调用该函数:

IE.Document.parentwindow.execscript "handleKeyDown({which:13,preventDefault:function(){}});"

Sub FillInternetForm()子 FillInternetForm()

Dim IE As Object将 IE 调暗为对象

Set IE = CreateObject("InternetExplorer.Application")设置 IE = CreateObject("InternetExplorer.Application")

IE.Navigate " http://reference.medscape.com/drug-interactionchecker " IE.Navigate “ http://reference.medscape.com/drug-interactionchecker

IE.Visible = True IE.可见 = 真

While IE.Busy IE.Busy 时

DoEvents做事件

Wend文德

Set TrackID = IE.Document.getelementbyid("MDICtextbox")设置 TrackID = IE.Document.getelementbyid("MDICtextbox")

TrackID.Value = "aspirin" TrackID.Value = "阿司匹林"

IE.Visible = True IE.可见 = 真

'Application.SendKeys ("{ENTER}") 'Application.SendKeys ("{ENTER}")

IE.Document.parentwindow.execscript "handleKeyDown({which:13,preventDefault:function(){}});" IE.Document.parentwindow.execscript "handleKeyDown({which:13,preventDefault:function(){}});"

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM