3

Below is the code which I'm using but I'm getting this error that:

object doesn't support this property or method

while using getElementsByClassName. The new 2 variable I'm using is not getting filled please help me out, do let me know if I'm doing it the wrong way.

Sub PopulateTasks()
'Variable Declaration
Dim ie As Object
Dim noTaskText As String
Set ie = CreateObject("InternetExplorer.Application")

url = "http://example/do/"
    .Visible = True
    .Navigate url
    .Top = 50
    .Left = 430
    .Height = 400
    .Width = 400
    Do Until Not ie.Busy And ie.readystate = 4
        DoEvents
    Loop
End With
Set link = ie.Document.getElementsByTagName("a")

For i = 1 To 200
        For Each l In link
            If l.innertext = storyIds(i) Then
                l.Click
                Do Until Not ie.Busy And ie.readystate = 4
                    DoEvents
                Loop
                If InStr("No tasks have been defined.", ie.Document.Body.outerText) <> 0 Then
                    noTaskFound = True
                End If
                noTaskText = ie.Document.getElementsByClassName("highlighted_message")(0).innerText

            If noTaskFound = True Then

            End If
        Next
            ie.Document.getElementbyId ("")                
            Do Until Not ie.Busy And ie.readystate = 4
                DoEvents
            Loop     
Next i    
End Sub
Aravind Scorpion
  • 31
  • 1
  • 1
  • 3
  • 3
    Not sure what you're trying to do with "`new2`" but you are missing an "s" after "element" in `getElementsByClassName()` – Porcupine911 Mar 13 '16 at 19:43
  • 1
    Two things I can think of: 1) the version of IE you are using does not support that method, and 2) after you `Click` the link elements, you need to *wait* for the page to load. – David Zemens Mar 13 '16 at 19:43
  • 1
    You don't `Set new2 = ...` to an `.innerText` property. Simply assign it to a string type var. `str = ie....innertext` (and declare your vars!) –  Mar 13 '16 at 19:51
  • in the html page there this entry

    No tasks have been defined.

    I am trying to check if this text is present in the html page, if yes I am doing further work on that. I have tried including the missing s in getElementsByClassName() had done amistake while copying here also have waited for the page to load.
    – Aravind Scorpion Mar 13 '16 at 20:08
  • Are you now taking David's advice and waiting for the page to load after clicking the link? You *have* to do that. If Yes it would be useful to update your question with your current code. – Tim Williams Mar 13 '16 at 21:31
  • Have added comments given by David and updated the code above, but in the InStr function too I am not getting the highlighted text value, not sure why, is that the reason I am getting this error?. but I have verified that the text is coming in source code of the HTML. Please advice. – Aravind Scorpion Mar 14 '16 at 08:17
  • You are using Instr incorrectly - should be `InStr(ie.Document.Body.outerText, "No tasks have been defined.")` As florentbr notes below though, if you can't provide a reachable URL then it's difficult to provide any useful help. – Tim Williams Mar 14 '16 at 16:09
  • Thanks Tim, had made that InStr mistake in haste, its workingfine now and regarding the link, I am afraid the link wont open at your end. – Aravind Scorpion Mar 14 '16 at 18:33

1 Answers1

3

To get an element with the class name, I would use querySelector:

Set element = ie.Document.querySelector(".classname")
Debug.Print element.innerHTML

With your example:

txt = ie.Document.querySelector(".highlighted_message").innerText
If txt = "No tasks have been defined." Then
    noTaskFound = True
    Exit For
End If
Florent B.
  • 41,537
  • 7
  • 86
  • 101