javascript - 如何模拟 "Focus"和 "typing"事件

标签 javascript node.js excel vba web-scraping

试图模拟 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/

相关文章:

javascript - 从多个 javascript 线程访问 IndexedDB

javascript - Angularjs,为什么这个 watch 在它的变量改变时没有反应?

javascript - 使内部阵列与外部阵列不同

node.js - Nodemailer/Gmail - 究竟什么是刷新 token ,我如何获得一个?

javascript - 如何使用 Node js读取上传到谷歌云存储的JSON文件的内容

javascript - 使用 POST 从数据库中检索指定的项目

vba - 剪切粘贴某行数据

vba - 使用每个单元格中间的字符按列排序,无需辅助列

vba - 如何停止VBA代码运行?

javascript - 如何设置默认 View 位置 (Cesium 1.6)