IE getElementbyId无效

时间:2016-09-29 04:29:58

标签: html vba internet-explorer getelementbyid

我正在尝试点击网页上的“a”元素,但我找不到它为什么不起作用。

这是我的VBA代码。

Function answer1(ie3 As InternetExplorer, str_anwer As String, answerid As String)

Dim ie4 As New InternetExplorer
Dim a As Object


Set ie4 = ie3


ie4.Document.getElementbyId("view=" & answerid).Click
ie4.Document.getElementbyId("reply_cont").Value = str_anwer



End Function

错误:找不到属性

以下是我认为位于

中的网页的HTML代码
<tr>
        <td class="thm">208975260</td>
        <td><pre>교환</pre></td>

        <td class="subject"><a href="#" onClick="return toggleDetail('208975260');" id="view208975260">작동이안되서 교환 원합니다 어떻게 하면되나요?</a></td>
        <td class="id"><span class="thm">st******</span><br>한혜진</td>
        <td class="thm">2016.09.29 12:53:57</td> 

            <td id="date208975260"><span class="point2 ls1">미답변</span>
            </td> 
        <td class="ansr">-</td>
    </tr>

抱歉我的英文

我的英语不流利。

请让我知道它为什么不起作用

1 个答案:

答案 0 :(得分:1)

如果没有引用 Microsoft Internet Controls(SHDocVw) Microsoft HTML Object Library ,代码可能如下所示。请注意IsNull电话。当像这样调用getElementbyId并且在页面上找不到该元素时,该函数返回Variant\Null

在注释代码中显示了第二个示例。在这种情况下,添加了引用,并在getElementbyId类型的变量上调用了HTMLDocument。如果在页面上找不到元素,则此函数返回Nothing

Sub main()
    Dim ie, url, readyStateComplete

    readyStateComplete = 4
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    url = "your-url"
    ie.navigate url

    While ie.Busy Or ie.readyState <> readyStateComplete: DoEvents: Wend

    answer1 ie, "<anwer>", "208975260"

    ie.Quit

End Sub

Function answer1(ie As Variant, str_anwer As String, answerid As String)
    Dim a As Object
    If Not IsNull(ie.Document.getElementbyId("view" & answerid)) Then
        ie.Document.getElementbyId("view" & answerid).Click
    End If

    If Not IsNull(ie.Document.getElementbyId("reply_cont")) Then
        ie.Document.getElementbyId("reply_cont").Value = str_anwer
    End If

'    Dim htmlDoc As HTMLDocument
'    Set htmlDoc = ie.document
'    If Not htmlDoc.getElementbyId("reply_cont") Is Nothing Then
'        htmlDoc.getElementbyId("reply_cont").Value = str_anwer
'    End If

End Function

了解Early/Late Binding之间的区别。