Home Excel VBA HTML automation using getElementsByClassName sometimes works and sometimes don't
Reply: 0

Excel VBA HTML automation using getElementsByClassName sometimes works and sometimes don't

user2195
1#
user2195 Published in June 18, 2018, 3:40 am

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?

Moreover, it always works when I run it by steps (F8).

HTML codes for Part 1:

 <div class="article-title">Some text in russian</div>

Part 2:

 <a href="javascript:void(0);" class="js-popup-open" data-
 popup="callback">Some text in russian 2</a>

Here is what i made so far:

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 <> 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 <> 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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302867 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO