如何模拟“焦点"?和“打字"活动 [英] How to simulate an "Focus" and "typing" Events

查看:25
本文介绍了如何模拟“焦点"?和“打字"活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试模拟 onfocus 和打字事件,但不起作用

Trying to simulate onfocus and typing event, but it not work

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 错误

How to call focus and typing events? Sendkeys is bad solution as it have Excel bug with Numlock

推荐答案

这些元素的事件侦听器指示正在监视输入事件.您可以创建这些然后触发.

The event listeners for those elements indicate input events are watched for. You can create those and then fire.

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工作正常,如下所示.安装 selenium 后,转到 VBE > Tools > References 并添加对 selenium 类型库的引用.您应该使用最新的 ChromeDriver.ChromeDriver 可能已经安装在 selenium 文件夹中 - 否则需要在那里添加.

If you are prepared to use selenium basic it works just fine as follows. After installing selenium go VBE > Tools > References and add a reference to selenium type library. You should use the latest ChromeDriver. The ChromeDriver may come installed already in the selenium folder - otherwise it needs to be added there.

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

这篇关于如何模拟“焦点"?和“打字"活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆