簡體   English   中英

Excel VBA Internet Explorer Onclick單擊下拉菜單

[英]Excel VBA Internet Explorer Onclick to click drop down menu

我試圖使用Excel VBA代碼單擊Internet Explorer中的下拉類型菜單。

這是HTML代碼:

<div class="treeNodeStyle" id="trMenu_14" nowrap="" style="visibility: visible;">
<div class="treeNodeWrapperStyle" nowrap="">
<a class="treeInlineStyle" onclick="FolderExpand('trMenu','14');return false;" href="">
<img align="top" class="treeInlineStyle" src="/visimages/tree/plus.gif" border="0">
</a> 
<div class="treeSelectorStyle" onclick="selectNode('trMenu','14',false)" ondblclick="dblselectNode('trMenu','14')">
<img align="absmiddle" class="treeInlineStyleImg" src="/visimages/tree/folder.gif" border="0">
<div title="Finance" class="treeNodeTextStyle" nowrap="true"> Finance</div>

這是我試圖使用的VBA代碼。 試圖了解我是否應該引用ID或類。

Dim btn as Object

For Each btn In IE.Document.getElementsByClassName("treeNodeStyle")
    If btn.innerText = "Finance" Then
        btn.Click
        Exit For
    End If
Next btn

我無法獲取代碼來在HTML中找到任何類型的元素。

我將假設您要擴展文件夾,但它有助於指示哪個元素需要接收點擊。

為此,我使用帶有* contains運算符的css attribute = value選擇器來根據其值的一部分來定位onclick屬性

ie.document.querySelector("[onclick*=FolderExpand]").click

如果事件需要觸發那么

ie.document.querySelector("[onclick*=FolderExpand]").FireEvent "onclick"

可以應用類似的原則來定位具有onclickondblclick屬性的其他元素。

finance元素可以通過其title屬性進行定位

ie.document.querySelector("[title=Finance]")

但我不確定這是否是點擊的目標。

從您的HTML代碼看起來, Finance div位於父div中,其父類名為treeSelectorStyle,並且它具有onclick事件。

因此,您可以嘗試使用其類名單擊父div可能有助於解決問題。

示例代碼:

Sub demo()

    Dim URL As String
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = "C:\Users\Administrator\Desktop\152.html"   'chnage the URL here...
    IE.Navigate URL

    Do While IE.ReadyState = 4: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop

    IE.document.getelementsbyclassname("treeSelectorStyle").Item(0).Click
    Set IE = Nothing
End Sub

這里基於示例代碼,我單擊Item(0),但您需要更改具有treeSelectorStyle類的元素的索引號以單擊正確的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM