使用Excel VBA自动化IE:不在网站中输入

时间:2018-05-07 10:48:50

标签: excel-vba internet-explorer-9 vba excel

我已成功浏览内部网站点:按钮,标签,下拉菜单等... 还有一个问题是:打字。

我可以像往常一样在导航网站中定期写字段。例如,在登录页面中:

IE.document.getElementsByTagName("INPUT")(0).Value = myId
IE.document.getElementsByTagName("INPUT")(1).Value = myPassword
IE.document.getElementsByTagName("button")(0).Click

不幸的是,在这种情况下,尽管填写了字段,但当我点击提交时系统会收到此错误:

“所有字段必须填写”

So, I've tried to complete the fields character by character, but the result is the same.


Dim HTMLdoc As HTMLDocument
Set HTMLdoc = IE.document
Dim evtKeyDown As Object
Set evtKeyDown = HTMLdoc.createEvent("keyboardevent")
evtKeyDown.initEvent "keydown", True, False
'******************************
Dim i As Long
Dim userInput As HTMLInputElement
Set userInput = HTMLdoc.getElementsByTagName("input")(0)
userInput.Value = ""
For i = 1 To Len(myId)
  userInput.Value = userInput.Value & Mid(myId, i, 1)
  userInput.dispatchEvent evtKeyDown 
  DoEvents
Next
'******************************
Dim j As Long
Dim userInput2 As HTMLInputElement
Set userInput2 = HTMLdoc.getElementsByTagName("input")(1)
userInput2.Value = ""
For j = 1 To Len(myCode)
  userInput2.Value = userInput2.Value & Mid(myPassword, j, 1)
  userInput2.dispatchEvent evtKeyDown 
  DoEvents
Next
'******************************
IE.document.getElementsByTagName("button")(0).Click

可能事件没有很好地启动,或者它不是创建的正确事件。

无论如何,我注意到html代码中有一个不寻常的变化:

<tr id="access123" class="row" style="width:330px;">
input id="log" class="textbox" type="text" maxlength="18" value=""

当我点击输入字段时,html代码变为:

<tr id="access123" class="row-row-over" style="width:330px;">
input id="log" class="textbox-textbox-focus" type="text" maxlength="18" value="">

0 个答案:

没有答案