试图模拟 onfocus 和 typing 事件,但它不起作用
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 是一个糟糕的解决方案,因为它有 Excel 与 Numlock 的错误
最佳答案
这些元素的事件监听器表示正在监视输入事件。您可以创建这些然后开火。
Internet Explorer:
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 :
如果你准备使用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
关于javascript - 如何模拟 "Focus"和 "typing"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54763526/