2016-09-29 148 views
0

我试图点击网页上的'a'元素,但我无法找出为什么它不起作用。IE getElementbyId无法正常工作

这是我的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> 

对不起,我的英语

我不流利的英语。

请让我知道为什么它不工作

+0

在'getElementbyId(“view =”&answerid)''=''不应该在那里。结果应该是'id =“view208975260”'not'id =“view = 208975260”'。 – dee

+0

谢谢。我修好了,但不工作.... ie4.Document.getElementbyId(“查看”和答案ID)。点击这里给我发消息 错误:未找到物业 –

回答

1

没有参考Microsoft Internet控制(SHDOCVW)Microsoft HTML对象库的代码可能看起来像下面这样。请注意拨打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之间的区别。

+0

谢谢!它对我很有帮助! –

+0

我很高兴它有帮助! – dee