Nam Seong Sik
Nam Seong Sik

Reputation: 3

IE getElementbyId not working

I'm trying to click 'a' element on a webpage and I can't find out why it does not work.

Here is my VBA code.

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

Error: Property not found

Here's the HTML code from the webpage I think it is located in

<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>

Sorry for my English

I'm not fluent English.

Please, let me know why it is not working

Upvotes: 0

Views: 3602

Answers (1)

gembird
gembird

Reputation: 14053

Without reference to Microsoft Internet Controls (SHDocVw) and Microsoft HTML Object Library the code could look something like the following. Note the IsNull call. When getElementbyId is called like this and the element is not found on the page this function returns Variant\Null.

In commented code a second example is shown. In this case the references were added and getElementbyId was called on variable of type HTMLDocument. Here in case the element was not found on the page this function returns 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

Read about difference between Early/Late Binding.

Upvotes: 1

Related Questions