【问题标题】:IE getElementbyId not workingIE getElementbyId 不工作
【发布时间】:2016-09-29 04:29:58
【问题描述】:

我正在尝试单击网页上的“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>

对不起我的英语

我的英语不流利。

请告诉我为什么它不起作用

【问题讨论】:

  • getElementbyId("view=" &amp; answerid) 中,= 不应该存在。结果应该是id="view208975260" 而不是id="view=208975260"
  • 谢谢。我修复了它但没有工作.... ie4.Document.getElementbyId("view" & answerid).Click erro:Property not found

标签: html vba internet-explorer getelementbyid


【解决方案1】:

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

在注释代码中显示了第二个示例。在这种情况下,添加了引用并在HTMLDocument 类型的变量上调用了getElementbyId。如果在页面上找不到该元素,此函数将返回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 之间的区别。

【讨论】:

  • 我很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 2010-10-08
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多