我正在填写网络表单。并且需要键盘按。所以我用sendkeys击选项卡。第一次可以完美运行,但是第二次循环时,它会在Application.SendKeys "{TAB}", True
处停止/暂停。我必须在VBA中单击,然后再次恢复。
谢谢。
对于i = 2到3
IE.getElementsByName("recipientName")(0).Value = ActiveSheet.Range("A" & i)
AppActivate ("Internet Explorer"), True
Application.Wait (Now + TimeValue("00:00:03"))
For tabbtn = 1 To 20
Application.SendKeys "{TAB}", True
Application.Wait (Now + TimeValue("00:00:01"))
Next Tabbtb
Next i
End Sub
我尝试检查该站点,并且我了解到您正在尝试将值添加到文本框中,但是在提交数据时,它显示出必填字段。
20次传递TAB密钥不是解决此问题的正确方法。
您需要在字段中设置值后调度事件。
注意:您需要为添加值的每个字段调度事件。
我无权访问该特定页面,因此我对其登录页面进行了测试。该代码的结构在整个站点中都是相同的。
[这里,我只是向您演示如何分配元素事件的演示。
有2个文本框。 1用户名2密码。我将仅尝试为Username调度事件。您会注意到,它不会显示“用户名”的必填验证,但会显示“密码”的必填验证。
示例代码:
Option Explicit
'It works with Excel 32 bit and Excel 64 bit
#If Win64 Then
'For 64 bit systems
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
'For 32 bit systems
' Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub test()
Dim url As String
Dim browser As Object
Dim btn As Object
Dim txt As Object
Dim psw As Object
url = "https://www.myhermes.co.uk/login.html#/" 'Please modify the URL here...
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
Do Until browser.readyState = 4: DoEvents: Loop
'Stop
Set txt = browser.document.getElementById("existingUser") ' Please modify the ID of the element here...
txt.Focus
txt.Value = "[email protected]"
Set psw = browser.document.getElementById("existingPassword")
psw.Value = "12345"
Call TriggerEvent(browser.document, txt, "change")
Set btn = browser.document.getElementById("loginButton") ' This is the demo submit button. You can modify this code as per your own requirements...
btn.Click
End Sub
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
输出:
您需要在输入值的页面上执行类似的操作来解决此问题。您可以根据需要修改代码示例。