【问题标题】:Fill user name and password in a webpage using VBA使用 VBA 在网页中填写用户名和密码
【发布时间】:2014-07-25 03:36:21
【问题描述】:

这是我第一次尝试通过 VBA 浏览 IE 浏览器。 我在尝试着: - 去这个网页https://hb2.bankleumi.co.il/e/Login.html - 填写用户名 - 填写密码 - 点击“登录”按钮 现在我收到错误“对象'IWebBrowser2'的方法'文档'失败”

我试图检查网页 html 代码中的元素并找到它们的 id,但我想我在调用方法或接近对象时犯了一些错误。

我的代码是:

Option Explicit
Sub dataFromLeumi()
    Dim myPassword As String
    myPassword = "somepassword"
    Dim myUserName As String
    myUserName = "someusername"



    Dim loginPath As String
    loginPath = "https://hb2.bankleumi.co.il/e/Login.html"
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.application")
    IE.Visible = True
    IE.navigate loginPath

    Dim userName As Object
    Set userName = IE.document.getattribute("uid")
    userName.Item(0).Value = myUserName

    Dim password As Object
    password = IE.document.getelementbyid("password")
    password.Item(0).Value = myPassword


    IE.document.getelementbyid("enter").Click
End Sub

我应该对我的代码进行哪些更改?我错过了什么?

谢谢!

【问题讨论】:

    标签: javascript html vba dom


    【解决方案1】:

    试试这个

    Sub test()
    ' open IE, navigate to the desired page and loop until fully loaded
        Set ie = CreateObject("InternetExplorer.Application")
        my_url = "https://hb2.bankleumi.co.il/e/Login.html"
    
        With ie
            .Visible = True
            .Navigate my_url
            .Top = 50
            .Left = 530
            .Height = 400
            .Width = 400
    
        Do Until Not ie.Busy And ie.readyState = 4
            DoEvents
        Loop
    
        End With
    
    ' Input the userid and password
        ie.Document.getElementById("uid").Value = "testID"
        ie.Document.getElementById("password").Value = "testPW"
    
    ' Click the "Search" button
        ie.Document.getElementById("enter").Click
    
        Do Until Not ie.Busy And ie.readyState = 4
            DoEvents
        Loop
    End Sub
    

    【讨论】:

    • 完美!谢谢罗恩!
    • 接受答案不同于投票。每个 OP 都可以“接受”一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2014-07-22
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    相关资源
    最近更新 更多