如何在VBA中使用getElementByID?

时间:2015-11-26 17:40:50

标签: excel vba excel-vba

所以我有这个测试,首先用输入框询问Facebook的电子邮件和密码。然后它应该在Internet Explorer上访问(www.facebook.com)并使用您的电子邮件和密码登录,但它会给我错误(运行时错误'424'需要对象)。我不知道这段代码有什么问题,所以如果你能提供帮助,我会很高兴。

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

FB_ID = InputBox("Give me email for FB")
FB_PW = InputBox("Give me password for FB")
URL = "Facebook.com"

Site.navigate URL
While Site.busy
    Wend

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

While Site.busy
    Wend
Site.Visible = True

End Sub

提前谢谢大家的回答。

1 个答案:

答案 0 :(得分:2)

也许这会有所帮助?

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

    FB_ID = InputBox("Give me email for FB")
    FB_PW = InputBox("Give me password for FB")
    URL = "Facebook.com"

    Site.navigate URL
    While Site.busy
    Wend

    Dim oHTMLDoc As Object
    Set oHTMLDoc = Site.document

    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