登录网站错误:getElementById运行时错误91

问题描述 投票:2回答:1

我有VBA代码可登录Facebook。当我开始时,我有一个

运行时错误91“对象变量或未设置块变量”。

当我调试时一切正常。

Option Explicit

Sub FB_Login()
    Dim Site As Object
    Set Site = CreateObject("InternetExplorer.application")
    Dim FB_ID As String
    Dim FB_PW As String
    Dim URL As String
    Dim oHTMLDoc As Object

    FB_ID = InputBox("Podaj adress e-mail do logowania")
    FB_PW = InputBox("Podaj Hasło FB")

    Site.Visible = False

    URL = "Facebook.com"

    Site.navigate URL
    While Site.busy
    Wend

    Set oHTMLDoc = Site.document

    oHTMLDoc.getElementById("email").Value = ""    'Gives error
    oHTMLDoc.getElementById("email").Value = FB_ID    'Gives error
    oHTMLDoc.getElementById("pass").Value = FB_PW  'Run-time error '424'
    oHTMLDoc.getElementById("loginbutton").Click   'Object required

    While Site.busy
    Wend
    Site.Visible = True
End Sub
vba internet-explorer
1个回答
0
投票

在下面尝试此代码:

创建一个模块并将此代码放在下面:

Public Site As New InternetExplorer

这是您的新fb_login函数:

    Dim FB_ID As String
    Dim FB_PW As String
    Dim URL As String
    Dim oHTMLDoc As Object


    URL = "Facebook.com"

    FB_ID = InputBox("Podaj adress e-mail do logowania")
    FB_PW = InputBox("Podaj Haslo FB")


    Site.Navigate2 URL
    Site.Visible = True

    WaitWhileBrowserIsLoading 1500

    Set oHTMLDoc = Site.document

    oHTMLDoc.getElementById("email").Value = FB_ID
    oHTMLDoc.getElementById("pass").Value = FB_PW
    oHTMLDoc.getElementById("loginbutton").Click

在下面包含此功能以检查浏览器的状态:

Public Sub WaitWhileBrowserIsLoading(Optional intWaitForBrowserToBecomeBusy As Integer = 0, Optional timeOutSeconds As Double = 0)
    With Site
        'wait for the browser to become busy to avoid skipping through without checking
        If intWaitForBrowserToBecomeBusy = 0 Then
            Sleep 800
        Else
            Sleep intWaitForBrowserToBecomeBusy
        End If

        Dim startTime As Double
        startTime = Timer
        Dim timePassed As Double
        timePassed = 0
        'Wait IE
        Do While .readyState <> READYSTATE_COMPLETE
            Sleep 100
            DoEvents
            If timeOutSeconds <> 0 Then
                timePassed = Timer - startTime
                If timePassed > timeOutSeconds Then Exit Sub
            End If
        Loop
        'Wait document
        Do While .document.readyState <> "complete"
            Sleep 100
        Loop
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.