2016-09-29 146 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