[英]How to simulate an "Focus" and "typing" Events
试图模拟 onfocus 和打字事件,但它不起作用
Sub Login(MyLogin, MyPass)
Dim IEapp As InternetExplorer
Dim IeDoc As Object
Dim ieTable As Object
TaskKill "iexplore.exe"
Set IEapp = New InternetExplorer
IEapp.Visible = True
IEapp.Navigate "https://example.com/portal/en/login"
Do While IEapp.Busy: DoEvents: Loop: Do Until IEapp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = IEapp.Document
With IeDoc.forms(2)
.Name.Value = MyLogin
.Name.Focus
.FireEvent ("onkeypress")
.FireEvent ("onchange")
.Password.Value = MyPass
.Password.Focus
.FireEvent ("onkeypress")
.FireEvent ("onchange")
End With
IeDoc.getElementsByClassName("form__button form__button--login-site")(1).Click
End Sub
如何调用焦点和打字事件? Sendkeys 是一个糟糕的解决方案,因为它有 Numlock 的 Excel 错误
这些元素的事件侦听器指示正在监视输入事件。 你可以创建这些然后开火。
IE浏览器:
Option Explicit
Public Sub LogIn()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.darsgo.si/portal/en/login"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector(".LoginHeader + p a").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim event_onInput As Object
Set event_onInput = .document.createEvent("HTMLEvents")
event_onInput.initEvent "input", True, False
With .document.querySelector("#name")
.Value = "bobBuilder@banana.com"
.dispatchEvent event_onInput
End With
With .document.querySelector("#password")
.Value = "something"
.dispatchEvent event_onInput
End With
.document.querySelector(".form__button").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
硒:
如果您准备使用selenium basic它可以正常工作,如下所示。 安装 selenium 后,转到 VBE > Tools > References 并添加对 selenium 类型库的引用。 您应该使用最新的 ChromeDriver。 ChromeDriver 可能已经安装在 selenium 文件夹中 - 否则需要将其添加到那里。
Option Explicit
Public Sub Login()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://www.darsgo.si/portal/en/login"
With d
.Start "Chrome"
.get URL
.FindElementByCss(".choose-language-popup__list li:nth-of-type(2) a").Click
.FindElementByCss(".choose-language-popup__icon-continue").Click
.FindElementByCss("p.registerHeader a").Click
.FindElementById("name").SendKeys "bob@builder.com"
.FindElementById("password").SendKeys "verySecret"
.FindElementByCss(".form__button").Click
Stop
.Quit
End With
End Sub
我认为这对你有用:
Sub Login()
Dim IEapp As InternetExplorer
Dim IeDoc as Object
Dim ieTable As Object
TaskKill "iexplore.exe"
Set IEapp = New InternetExplorer
IEapp.Visible = True
IEapp.navigate "https://example.com/portal/en/login"
Do While IEapp.Busy: DoEvents: Loop: Do Until IEapp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = IEapp.document
With IeDoc.forms(2)
.elements("name").Value = MyLogin
.elements("password").Value = MyPass
End With
IeDoc.forms(2).submit
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.