0

I am quite new to VBA/html, trying to automate filling a form, but cannot even call it ... I searched the web for weeks tried many different code combinations, but none got me there.

The HTML code/element looks like this:

div title:"TextText" class:"text-truncate mb-2" id="abc_sidebar_startmenuitem" data-type="record" data-appid="82" data url="index.cfm?Xxx=&Yyy=">

i class="fa icon-app-82 mr-1 fa-fw">/i>

span class="clickable" id="ext-eng123">Text/span>
/div>

Problem is that class="clickable" is 30th appearance of clickable on the page, id="ext-eng123" is built from the text ext-eng and 3 variable unknown digits, always different.

Example of VBA code used:

Sub GetClick()
    Dim ie As Object
    Set ie = CreateObject("internetexplorer.application")
    With ie
        .Visible = True
        .navigate "https://company.application.com/home/index.cfm?Tab=home"
        Do While .Busy
            DoEvents
        Loop
        Do While .readyState <> 4
            DoEvents
        Loop
    End With
    Dim objIE As Object
    objIE = document.getElementByClassName("clickable")(29)
    objIE.Click
End Sub

I tried over 10+ different code samples, including calling frame number, none worked, I am stuck.

Community
  • 1
  • 1
  • *"weeks"?* Please share some more of what you've tried, and the results. "None Worked" isn't much help. Errors? Where/when/why? What's the data look like, and what do you need it to look like? – ashleedawg Jun 08 '18 at 01:07
  • Get the element with id "abc_sidebar_startmenuitem" then call `getElementsByClassName("clickable")` on that element. – Tim Williams Jun 08 '18 at 05:26
  • Many thanks for replies. @Tim: "abc_sidebar_startmenuitem" and getElementsByClassName("clickable") appears on the page in 8 different combinations. it is 8 spans with id "ext-eng-+++" where +++ are 3 numbers, unique for each span, but whenever the page is click they are different. @ ashleedawg I will post som,e examples with err message below. weeks, but not full-time, couple hours every other day :) – Matus Kovac Jun 08 '18 at 19:15
  • `id` is supposed to be unique on any given page, so that sounds like it's a bit broken. – Tim Williams Jun 08 '18 at 19:21
  • Some more examples of codes that didn't work – Matus Kovac Jun 08 '18 at 20:00
  • Set Elements = Doc.GetElementsByTagName("span") For Each Element In Elements If Element.Innertext = "Inspection" Then .Click – Matus Kovac Jun 08 '18 at 20:01
  • Set clickButton = HTMLdoc.getElementsByClassName("clickable")(29) MsgBox clickButton.outerHTML Click.Button.dispatchEvent evtClick – Matus Kovac Jun 08 '18 at 20:06
  • Tim, ID is unique for each div/span+clickable. but it changes upon every visit. it is always a combination of "ext-eng-" and 3 numbers, like ext-eng-123. "clickable" appears 30 times in the script, of which 8 times with "abc_sidebar_startmenuitem", I need to call 2nd appearance – Matus Kovac Jun 08 '18 at 20:13

1 Answers1

2

Try the following code. It is hard to give any solution without playing with that site. They are always hypothetical.

Sub GetClick()
    Dim IE As New InternetExplorer, Html As HTMLDocument

    With IE
        .Visible = True
        .Navigate "https://company.application.com/home/index.cfm?Tab=home"
        While .Busy = True Or .ReadyState < 4: DoEvents: Wend
        Set Html = .Document
    End With

    ''If the problem still persists, make sure to put here some delay

    Html.querySelector(".clickable[id^='ext-eng']").Click
End Sub

Another approach might be something like (if the word "Text" didn't appear anywhere with the same class name):

For Each elem In Html.getElementsByClassName("clickable")
    If InStr(elem.innerText, "Text") > 0 Then elem.Click: Exit For
Next elem

Reference to add:

Microsoft Internet Controls
Microsoft HTML Object Library
SIM
  • 21,997
  • 5
  • 37
  • 109
  • Thank you SIM, but getting Run-time err 91. Obj or var not set. the "clickable" with "ext-eng" repeats 8 times on the page, mine is 2nd. – Matus Kovac Jun 08 '18 at 19:23
  • What about this `Html.querySelector("#abc_sidebar_startmenuitem span.clickable").Click`. Tim Williams has already suggested this. You have given us a very minor portion of html elements to provide you with a solution let alone they are badly formatted, i meant no close tags. – SIM Jun 08 '18 at 19:41
  • Same Sim. I know, my bad, but couldnt figure out how to post it completely given the rules for posting. Added image above if that could help... – Matus Kovac Jun 08 '18 at 21:00
  • Okay, let us try in some different ways. What is it written on that button. Is it the word `Text`? If it is just check if there is any other button with the same name `Text`. – SIM Jun 08 '18 at 21:11
  • If it breaks, there are still few ways to go. Just check if there is anything, i meant anything uniquely used here that has not been used elsewhere such as `data-type="record"`. – SIM Jun 08 '18 at 21:43
  • SIM, it works! The only problem is that with Html.querySelector("#gsw_sidebar_startmenuitem span.clickable").Click . I get to 1st of 8 start menu items, while I need to get the 2nd. I tried Html.querySelector("#gsw_sidebar_startmenuitem span.clickable")(1). Click but that didn't work. Thanks a lot anyway. – Matus Kovac Jun 08 '18 at 22:29
  • The right try should be `Html.querySelectorAll("#gsw_sidebar_startmenuitem span.clickable")(1).Click` – SIM Jun 08 '18 at 22:32
  • Hi SIM, both these options work (Html.querySelectorAll as well as If InStr(elem.innerText, "Text"), thanks a million! :) – Matus Kovac Jun 09 '18 at 18:41