【问题标题】:Automate click(expanding) plus signs within internet explorer在 Internet Explorer 中自动单击(扩展)加号
【发布时间】:2015-09-17 15:59:56
【问题描述】:

我正在致力于将一些数据自动输入到 Intranet 网页中。过去,我曾成功使用此类代码单击复选框,但无法使其在扩展行的加号上工作。下面的代码什么都不做,也不提示错误,代码运行正常。

这是我的代码:

Set div = IE.document.getElementsByTagName("div")

For Each i In div
    'showExpand?
    If i.id Like "iconShowA*" Then
        If i.onclick = "showExpand(*)" Then
            i.Click'Click plus sign
            v = Replace(i.id, "iconShowA", "")
            col.Add v 'store the numeric part
        End If
    End If
Next i

For Each v In col
    Debug.Print v
Next v

相关的 HTML 行是:

(我要点击的内容,其中可能有不同的数字标识符“iconShowA(x)”)

<div id="iconShowA34" class="ui-icon ui-icon-plusthick" onclick="showExpand('34','34')" ;="" style="display: block;"></div>

(我也需要避免点击这些)

<div id="iconShowA_N4" class="ui-icon ui-icon-plusthick" onclick="showExpandSub('4','4')" ;=""></div>

【问题讨论】:

  • 那么到底是什么问题呢?你有错误吗?它什么都不做吗?是不是点击了你不想点击的东西?

标签: vba excel internet-explorer ie-automation


【解决方案1】:

下面的代码能够达到预期的效果。我无法使 TagName 约定起作用。此方法使用 getElementByID 浏览网页。使用完整 ID 似乎很重要,因此我使用 Do While 循环来遍历 ID 命名约定中可能使用的数字。

n = DateDiff("ww", firstDate, secondDate)'Determines number of plus' to click
v = 0 'Counter for plus click event
x = 6 ' starting value for numerical piece of Id


Do While n > v 'continue loop until all plus' have been clicked
Set div = IE.document.getElementById("iconShowA" & x) 'Id must be defined completely to click
    If div Is Nothing Then 'tests if element exists
        x = x + 7
    Else
        div.Click 'clicks an element that exists
        v = v + 1
        x = x + 7 'iterates id number by 7 as that is convention of website
    End If
Loop

【讨论】:

    【解决方案2】:

    CSS 选择器:

    假设您要点击的所有元素都有showExpandSub 而不是showExpand,那么您可以使用CSS 选择器来选择这些元素:

    div[onclick^='showExpand(']
    

    这表示带有div 标签的元素具有onclick 属性,其值以'showExpand(' 开头。


    CSS 查询:


    VBA:

    使用文档的querySelectorAll 方法返回所有匹配元素的节点列表。然后循环.Length 以检索元素。

    Dim aNodeList As Object, iNode As Long
    Set aNodeList = ie.document.querySelectorAll("div[onclick^='showExpand(']")
    For iNode = 0 To aNodeList.Length - 1
        Debug.Print aNodeList.item(iNode).innerText
        'Debug.Print aNodeList(iNode).innerText '<== Sometimes this syntax
    Next iNode
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2012-11-01
      相关资源
      最近更新 更多