触发<a> Element Without ID or Text

时间:2018-02-18 05:53:33

标签: excel vba excel-vba

While setting up an automation process for extracting data from a page, I came across a link where I am having trouble selecting it. Here is the HTML code:

<a class="dt-button buttons-copy buttons-html5" tabindex="0" aria-controls="tblReport" href="#" title="Copy to Clipboard"><span><i class="fa fa-files-o fa-lg blue"></i></span></a>

My last attempt was using:

For Each m In objIE.document.getElementsByTagName("a")
    If m.className = "dt-button buttons-copy buttons-html5" Then
        m.Click
        Exit For
    End If
Next

With Dim m As HTMLElementCollection.

But nothing happens.

If triggered, a window will appear stating that the content has been copied to the clipboard.

Another approach was:

objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0).Click

But I get the error message

Object variable or With block variable not set

objIE is set as:

Dim objIE As Object

Set objIE = CreateObject("InternetExplorer.Application")

I can't figure out what I am missing.

1 个答案:

答案 0 :(得分:1)

您需要确保在设置对象之前完全加载页面。

如果您已经这样做了,有时网页会说实际上没有网页。为了避免这种情况,你可以通过这样做来循环你的对象,等待它准备就绪:

Do While objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0) Is Nothing
    DoEvents
Loop

然后当它不再是Nothing时,您现在可以点击它:

objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0).Click