VBA to click web button with dynamic elementID and repetitive class

我只是一个虾纸丫 提交于 2020-01-06 06:06:15

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/50751921/vba-to-click-web-button-with-dynamic-elementid-and-repetitive-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!