为 If 语句引用 'onclick' 事件 - IE 自动化

Referencing 'onclick' event for If statement - IE automation

如果这有点令人困惑,我们深表歉意。但是,我在这里有点力不从心。我有一个自动化 IE 的宏。我的宏有一次导航到用于下载报告的 URL。 URL 几乎总是相同的。但是,link 中的一位数字有时会发生变化。 link中变化的数字只能是12。获取报告按钮的 onclick 事件指示将生成哪个报告。因此,我需要我的宏有一个 IF 语句来读取 onclick,如果 onclick1,那么它将导航到 link 带有 1 或者如果它是 2,它将导航到带有 2 的 link。我在下面提供了 onclick 详细信息:

Link with 'ReporttypeID=1'

<input name="btnContinue" class="primary" id="btnContinue" onclick="javascript:pop('Splash.aspx?SplashID=3&amp;../Reports/ReportView.aspx&amp;ReporttypeID=1&amp;lang=EN&amp;ruletypeid=5&amp;ruleid=0'); return false;" type="submit" value="Continue">```

Link with 'ReporttypeID=2'

<input name="btnContinue" class="primary" id="btnContinue" onclick="javascript:pop('Splash.aspx?SplashID=3&amp;../Reports/ReportView.aspx&amp;ReporttypeID=2&amp;lang=EN&amp;ruletypeid=5&amp;ruleid=0'); return false;" type="submit" value="Continue">```

我的代码如下,如何call/reference onclick id?

Sub login()
    Application.DisplayAlerts = False
    Dim url As String
    url = UserForm.Path2.Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie 'Open IE
        .navigate url
        ieBusy ie
        .Visible = False

        ie.navigate "https://mylink.com/ReporttypeID=1" 'Currently navigate to this set link
    'Need IF statement to say if onclick = 1 then go to above link, else if it = 2 then go to the below link
        'ie.navigate "https://mylink.com/ReporttypeID=2"

        ieBusy ie

    End With 'End IE actions

    Application.DisplayAlerts = True
End Sub

这些答案是基于总是存在一个或另一个。

提取onclick属性,测试是否包含字符串"ReporttypeID=1"

If Instr(ie.document.getElementById("btnContinue").getAttribute("onclick"),"ReporttypeID=1")> 0 Then
    'report 1
Else
  'report 2
End If

另一种方法是使用 css attribute = value selector 和 contains 运算符来检查节点列表长度

If ie.document.querySelectorAll("[onclick*='ReporttypeID=1']").Length > 0 Then
    'report 1
Else
  'report 2
End If