Javascript 如何模拟“焦点”和“键入”事件

Javascript 如何模拟“焦点”和“键入”事件,javascript,node.js,excel,vba,web-scraping,Javascript,Node.js,Excel,Vba,Web Scraping,尝试模拟onfocus并键入事件,但不起作用 我的通行证 Dim IEapp作为InternetExplorer Dim IeDoc作为对象 可作为对象的 TaskKill iexplore.exe 设置IEapp=新的InternetExplorer IEapp.Visible=True IEapp.导航https://example.com/portal/en/login IEapp.Busy:DoEvents:Loop:Do直到IEapp.readyState=readyState\u完成

尝试模拟onfocus并键入事件,但不起作用

我的通行证 Dim IEapp作为InternetExplorer Dim IeDoc作为对象 可作为对象的 TaskKill iexplore.exe 设置IEapp=新的InternetExplorer IEapp.Visible=True IEapp.导航https://example.com/portal/en/login IEapp.Busy:DoEvents:Loop:Do直到IEapp.readyState=readyState\u完成:DoEvents:Loop 设置IeDoc=IEapp.Document 使用IeDoc.forms2 .Name.Value=MyLogin .Name.Focus .FireEvent onkeypress .FireEvent onchange .Password.Value=MyPass .Password.Focus .FireEvent onkeypress .FireEvent onchange 以 IeDoc.getElementsByClassNameform\uuuu按钮form\uuuu按钮login-site1.单击 端接头
如何调用焦点和键入事件?Sendkeys是一个糟糕的解决方案,因为它在Numlock中存在Excel错误

我认为这对您很有用:

子登录 Dim IEapp作为InternetExplorer Dim IeDoc作为对象 可作为对象的 TaskKill iexplore.exe 设置IEapp=新的InternetExplorer IEapp.Visible=True IEapp.navigatehttps://example.com/portal/en/login IEapp.Busy:DoEvents:Loop:Do直到IEapp.readyState=readyState\u完成:DoEvents:Loop 设置IeDoc=IEapp.document 使用IeDoc.forms2 .elementsname.Value=MyLogin .elementspassword.Value=MyPass 以 IeDoc.forms2.submit 端接头
我认为这会对你有用:

子登录 Dim IEapp作为InternetExplorer Dim IeDoc作为对象 可作为对象的 TaskKill iexplore.exe 设置IEapp=新的InternetExplorer IEapp.Visible=True IEapp.navigatehttps://example.com/portal/en/login IEapp.Busy:DoEvents:Loop:Do直到IEapp.readyState=readyState\u完成:DoEvents:Loop 设置IeDoc=IEapp.document 使用IeDoc.forms2 .elementsname.Value=MyLogin .elementspassword.Value=MyPass 以 IeDoc.forms2.submit 端接头
这些元素的事件侦听器指示监视输入事件。你可以创建这些,然后开火

Internet Explorer:


这些元素的事件侦听器指示监视输入事件。你可以创建这些,然后开火

Internet Explorer:



发生了什么事?最好在同一个循环中检查IEapp.Busy和IEapp.Readystate我得到了错误电子邮件地址是必须的,在我手动聚焦电子邮件并键入某个内容后,错误发生了哪些行按预期工作,即向表单添加数据?为什么在添加文本后要聚焦?因为这是页面对聚焦字段和键盘输入的要求,所以在添加文本之前可能需要聚焦?发生了什么?最好在同一个循环中检查IEapp.Busy和IEapp.Readystate我得到了错误电子邮件地址是必须的,在我手动聚焦电子邮件并键入某个内容后,错误发生了哪些行按预期工作,即向表单添加数据?为什么在添加文本后要聚焦?因为页面要求聚焦字段并从键盘键入,所以在添加文本之前可能需要聚焦?结果是相同的,因为页面希望用户聚焦并键入fieldform提交,但不打算登录,因为javascript不控制聚焦字段和键入字段,我认为提交是在单击之后进行的,但不是通过提交,因为这将是一种预防。default@DmitrijHolkin,那太不幸了。值得一试。我对VBA internet连接非常陌生,我很想知道Qharr提供的解决方案是否适用于您。结果相同,因为页面希望用户关注并键入fieldform提交,但不打算登录,因为没有javascript控制的关注和键入字段,我认为提交是在单击之后进行的,但不是通过提交,因为这将是一种预防。default@DmitrijHolkin,那太不幸了。值得一试。我对VBA internet连接非常陌生,非常想知道Qharr提供的解决方案是否适合您。该页面有事件侦听器,用于监视输入事件的这些元素。代码只是将这些事件添加到页面中,并在输入值后触发它们。这是通过向字段添加文本手动触发的。请参见以下内容:注意右上角搜索框中突出显示的元素和选择器注意,从窗口底部可以看到元素路径。这表明实际上有一个更好的css选择器:p.registerHeader aahhhh现在我明白了。我很困惑,因为其他元素的名称和密码对我来说太清楚了。我没有css/java/HTML背景,只是练习了VBA,但这些类型的东西仍然让我感到惊讶!谢谢你的解释。我希望将来能够使用它!另外,别忘了编辑你的。退出;非常感谢。请参见以下内容:并与
该页面中的事件侦听器监视这些元素的输入事件。代码只是将这些事件添加到页面中,并在输入值后触发它们。这是通过向字段添加文本手动触发的。请参见以下内容:注意右上角搜索框中突出显示的元素和选择器注意,从窗口底部可以看到元素路径。这表明实际上有一个更好的css选择器:p.registerHeader aahhhh现在我明白了。我很困惑,因为其他元素的名称和密码对我来说太清楚了。我没有css/java/HTML背景,只是练习了VBA,但这些类型的东西仍然让我感到惊讶!谢谢你的解释。我希望将来能够使用它!另外,别忘了编辑你的。退出;非常感谢。请看以下内容:然后玩这个
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
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