VBA单击具有动态elementID和重复类的Web按钮 [英] VBA to click web button with dynamic elementID and repetitive class

查看:76
本文介绍了VBA单击具有动态elementID和重复类的Web按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对VBA/html还是很陌生,试图自动填写表格,但是甚至无法调用它……我在网上搜索了 weeks ,尝试了许多不同的代码组合,但没有一个让我明白在那里.

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.

HTML代码/元素如下所示:

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>

问题是 class ="clickable" 是页面上第30个可点击的外观, id ="ext-eng123" 是根据文本 ext构建的-eng 和3个可变的未知数字,总是不同.

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.

使用的VBA代码示例:

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

我尝试了10多种不同的代码示例,包括调用帧号,但没有成功,我被困住了.

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

推荐答案

尝试以下代码.如果不使用该站点,很难给出任何解决方案.他们总是假想的.

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

另一种方法可能是类似的(如果单词"Text"没有出现在具有相同类名的任何地方):

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

添加参考:

Microsoft Internet Controls
Microsoft HTML Object Library

这篇关于VBA单击具有动态elementID和重复类的Web按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆