title: Excel VBA HTML automation using getElementsByClassName sometimes works and sometimes don't
body: <p>I've made a macro that opens Internet Explorer, navigates to a particular web-page with a list of articles (each article has the same class, but different innertext), and clicks a particular article. So far everything works perfectly well (Part 1 in a code below). However, when I try to do exactly the same operation (but with different class and innertext) again (Part 2 in a code below) it either works or not absolutely (as I see it) randomly. Maybe someone can give me a clue what may make it run smoothly every time?</p>
  
  <p>Moreover, it always works when I run it by steps (F8).</p>
  
  <p>HTML codes for
  Part 1:</p>
  
  <pre><code> &lt;div class="article-title"&gt;Some text in russian&lt;/div&gt;
  </code></pre>
  
  <p>Part 2:</p>
  
  <pre><code> &lt;a href="javascript:void(0);" class="js-popup-open" data-
   popup="callback"&gt;Some text in russian 2&lt;/a&gt;
  </code></pre>
  
  <p>Here is what i made so far:</p>
  
  <pre><code>Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds 
  As Long)
  Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As 
  Long, ByVal nCmdShow As Long) As Long
  Private Const SW_SHOWMAXIMIZED = 3
  
  Sub sup()
  
  Dim aEle As HTMLLinkElement
  Dim aaEle As HTMLLinkElement
  
  Dim objIE As Object
  
  Dim hwnd As Long
  
  Set objIE = CreateObject("InternetExplorer.Application")
  objIE.Visible = True
  
  
    objIE.navigate "http://www.spark-interfax.ru/ru/services"
  
  ShowWindow objIE.hwnd, SW_SHOWMAXIMIZED
    Do While objIE.Busy = True Or objIE.readyState &lt;&gt; 4: DoEvents: Loop
  'Sleep (3000) (tried both variants, doesnt help)
  
  'Part 1
  
   For Each aEle In objIE.document.getElementsByClassName("article-title")
  
      Debug.Print aEle.innerText
       On Error Resume Next
  
      If aEle.innerText = "Some text in russian" Then Exit For
      Next aEle
  
     aEle.Click
  
     Do While objIE.Busy = True Or objIE.readyState &lt;&gt; 4: DoEvents: Loop
  '  Sleep (3000) (again, tried both)
  
    'Part 2
     For Each aaEle In objIE.document.getElementsByClassName("js-popup-open")
  
      Debug.Print aaEle.innerText
       On Error Resume Next
  
      If aaEle.innerText = "some text in russian 2" Then  Exit For
      Next aaEle
      aaEle.Click
  
      End Sub
  </code></pre>