简体   繁体   English

如何使Excel VBA自动在IE中单击javascript a href链接

[英]How to make Excel VBA automate clicking javascript a href link in IE

I have a macro in VBA which uses Excel to automate navigating to a web-page. 我在VBA中有一个宏,该宏使用Excel自动导航到网页。

I am able to select elements by their ID and click or insert values to automate the site. 我能够通过其ID选择元素,然后单击或插入值以使站点自动化。

I am stuck however when trying to 'click' a link/button which does not seem to have any ID. 但是,在尝试“单击”似乎没有任何ID的链接/按钮时,我陷入了困境。 It is a JavaScript link: 这是一个JavaScript链接:

<div>
<table class="table" id="ctl00_cphMaster_Results" style="width: 100%; border-collapse: collapse;" cellspacing="0">
    <tbody><tr>
        <th style="width: 70px; white-space: nowrap;" scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Indicator')">True?</a></th><th class="hidden" style="width: 100px;" scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Reference')">Reference</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Results')">Result Number</a></th><th class="hidden" scope="col">&nbsp;</th><th scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Type')">Type</a></th><th class="hidden" scope="col"></th><th class="hidden" scope="col">&nbsp;</th><th class="hidden" scope="col">&nbsp;</th>
    </tr><tr>
        <td align="center"><a href="Summary02.aspx?"><img title="Accepted" id="true" style="border: currentColor; border-image: none;" alt="Accepted" src="/Common/Graphics/tick_green.gif"></a><img id="Accepted" style="border: currentColor; border-image: none;" src="/Common/Graphics/tick_spacer.gif"><img id="Accepted" style="border: currentColor; border-image: none;" src="/Common/Graphics/tick_spacer.gif"></td><td class="hidden">MyValue</td><td><a href="javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')">ResultNumber</a></td>
    </tr>
</tbody></table>

I have attempted to click 'ResultNumber$0' or trigger it in some way but so far have been unsuccessful. 我试图单击“ ResultNumber $ 0”或以某种方式触发它,但到目前为止未成功。 How can I automate IE to click this javascript link/button? 如何使IE自动单击此javascript链接/按钮? My code so far: 到目前为止,我的代码:

Sub ClickJavaLink()
Const URL = "https://myurl"    
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")

With ie
    .navigate URL
    ieBusy ie
    .Visible = True

    For Each Item In ie.document.all
        If Item.ID = "QuickSearchField" Then
            Item.Value = 1
        End If
        If Item.ID = "QuickSearch" Then
            Item.Value = "MyValue"
        End If
        If Item.ID = "btnSearch" Then
            Item.Click
            Exit For
        End If
    Next
    ieBusy ie

'code working up to this point - below code is attempt to click the javascript link but does not work   
    For Each Item In ie.document.all
        If Item.ID = "ResultNumber$0" Then
            Item.Click
            Exit For
        End If
    Next
    ieBusy ie
End With

End Sub 结束子

Sub ieBusy(ie As Object) Do While ie.Busy Or ie.ReadyState < 4 DoEvents Loop End Sub Sub ieBusy(即,作为对象)在while.Busy或ie.ReadyState <4 DoEvents循环结束时执行

There is a better, faster, way. 有一种更好,更快的方法。 Use an attribute = value css selector with contains operator 使用属性=值 CSS选择器和包含运算符

ie.document.querySelector("[href*='ResultNumber$0']").click

Single line; 单线; no looping; 没有循环; reduced code complexity. 降低代码复杂度。

You can reach the anchor elements by first accessing the table which conveniently enough has a unique ID . 您可以通过首先访问足够方便的具有唯一ID的表来访问锚元素。

Here's how you can loop through all anchor elements inside that table: 这是您如何遍历该表内的所有锚点元素的方法:

Dim anc As HTMLAnchorElement
For Each anc In ie.Document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")
    Debug.Print anc.innerText
    'anc.click 'uncomment this to click on all links
Next anc

For demonstration purposes the above code prints the text of these elements in the immediate window. 为了演示起见,以上代码在立即窗口中打印了这些元素的文本。

To access a specific element you can use the element's index. 要访问特定元素,可以使用元素的索引。 For example to access the first element in the table you can do it like so: 例如,要访问表中的第一个元素,您可以这样操作:

ie.Document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")(0).click

To access the second one you just replace 0 with 1 and so on. 要访问第二个,只需将0替换为1 ,依此类推。

Another way to access a specific element would be to use its href attribute: 访问特定元素的另一种方法是使用其href属性:

Dim anc As HTMLAnchorElement
For Each anc In ie.document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")
    If anc.href = "javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')" Then
        Debug.Print anc.innerText
        'anc.click
    End If
Next anc

You will need to add a reference (VBE>Tools>References) to Microsoft HTML Object Library . 您将需要向Microsoft HTML Object Library添加参考(VBE> Tools> References)。

You can try locating the href element on the page like this : 您可以尝试在页面上找到href元素,如下所示:

For Each item In Ie.Document.getAttribute("href", 2)

   If item.href ="javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')" Then

      item.Click

         Exit For

   End If

Next item

Let me know if that works. 让我知道是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM