首页 > 解决方案 > Excel VBA单击html div类中的项目

问题描述

我是第一个使用 Excel VBA 进行 Web 自动化的新手。到目前为止,我能够取得进展,但在尝试点击网站上看起来像按钮的东西时遇到了困难。如果你能帮助我,请告诉我,我很感激。谢谢!特定元素位于以下 html 代码中的 a 标记内:

<div class="sh-c-btn-group">
    <input name="pf.ok" type="hidden" value="">
    <input name="pf.cancel" type="hidden" value="">
    <a title="Sign In" class="sh-c-btn sh-c-btn--primary" onclick="postOk();">Sign In</a>
</div>

标签: excelvbawebautomation

解决方案


Jenn 和 Jumpgroup,非常感谢您的帮助 :) 通过获取标题和类名,设法通过以下循环来做到这一点:

Dim HTMLas As MSHTML.IHTMLElementCollection
Dim HTMLa As MSHTML.IHTMLElement

Set HTMLas = HTMLDoc.getElementsByTagName("a")

For Each HTMLa In HTMLas
    If HTMLa.getAttribute("title") = "Sign In" And HTMLa.getAttribute("classname") = "sh-c-btn sh-c-btn--primary" Then
        HTMLa.Click
    Exit For
    End If
Next HTMLa

推荐阅读