如何模拟“焦点”和“类型”事件

时间:2019-02-19 10:04:13

标签: javascript node.js excel vba web-scraping

尝试模拟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错误

2 个答案:

答案 0 :(得分:2)

这些元素的事件侦听器指示监视输入事件。您可以创建它们,然后解雇。

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 basic,则可以按如下所示正常工作。 安装硒后,转到VBE>工具>引用,然后添加对硒类型库的引用。您应该使用最新的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

答案 1 :(得分:1)

我认为这对您有用:

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