0

我正在致力于将一些数据自动输入到 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>
4

2 回答 2

0

CSS 选择器:

假设您想要点击的所有元素都有showExpandSubshowExpand那么您可以使用 CSS 选择器来选择这些元素:

div[onclick^='showExpand(']

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


CSS查询:

CSS 查询


VBA:

使用querySelectorAlldocument 的方法返回所有匹配元素的 nodeList。然后循环.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
于 2018-06-30T08:13:35.597 回答
0

下面的代码能够达到预期的结果。我无法使 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
于 2015-09-21T14:59:07.007 回答