为什么我不能使用GetElementById在页面上找到元素?

时间:2014-08-22 13:17:54

标签: javascript html vba dom getelementbyid

您好我正在使用VBA处理Internet Explorer自动化解决方案。检查我的网页并检查感兴趣的元素以确定其ID。 IE.document.GetElementByID方法失败,因为无法找到ID。我应该补充一点,网页是动态的。所以我想我应该等一下让javascripts完成运行。我停止执行VBA代码1分钟,但这没有用。

我还将.body.innerHTML.body.outerHTML的内容分配给文本文件以检查内容,但该元素无处可寻。我还保存为一个完整的网页,并查看了使用文本编辑器生成的html文件,但页面上的元素(以及更多)仍然缺失。

编辑:添加示例代码

Sub Test_Login()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    ie.Navigate2 "http://www.example.com"
    Do
        If ie.ReadyState = 4 Then
        ie.Visible = True
        Exit Do
        Else

        DoEvents
        End If
    Loop
    Application.Wait Now + TimeSerial(0, 0, 20)
    ie.document.getElementById("commandSignIn").Click
End Sub

1 个答案:

答案 0 :(得分:0)

这是我用来处理IE项目的方法:

IE.Document.All.Item("InputField")

要等到网站加载:

Do Until Not .Busy
    ' nothing needed here
Loop

从Internet Explorer登录网站的完整代码

Private Sub LoginToWebsite()
    ' needs references to
    ' microsoft internet controls
    ' microsoft HTML object library

     ' Prepare to open the web page
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .Navigate "www.WebsiteToLoginTo.com"

         '  Loop until the page is fully loaded
        Do Until Not .Busy
            ' nothing needed here
        Loop

         ' Make the desired selections on the web page and click the submit Button

        ' get handle on user name item and enter value
        Set webItem = IE.Document.all.Item("UserNameEntry")
        webItem.Value = "username"

        ' get handle on password item and enter value
        Set webItem = IE.Document.all.Item("PasswordEntry")
        webItem.Value = "password"

        ' get handle on submit button item and click
        Set webItem = IE.Document.all.Item("SubmitButton")
        webItem.Value = "submit"
        webItem.Click

         ' Loop until the page is fully loaded
        Do Until Not .Busy
            ' nothing needed here
        Loop         

    End With

End Sub