使用VBA在网页中填写用户名和密码

3
这是我第一次尝试通过VBA导航IE浏览器。 我正在尝试: - 前往此网页https://hb2.bankleumi.co.il/e/Login.html - 填写用户名 - 填写密码 - 点击“登录”按钮 但现在我收到的错误提示是“对象'IWebBrowser2'的方法'Document'失败”。
我尝试检查网页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

我需要在代码中做出哪些改变?我错过了什么?

谢谢!

1个回答

10

试一试

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

太好了!谢谢Ron! - Max Segal
接受答案与投票不同。每个问题的提出者都可以“接受”一个答案。 - ron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接