0

嗨,我正在尝试找出一种方法来调用映射到按钮的 java 脚本,使用 VBA 作为网页数据输入自动化的一部分。以下是该站点的代码以及我正在处理的代码。

浏览器代码:

<input name="reportIIS" value="Select" class="button" onclick="javascript: onSelect('/cooapp/servlet/CooMainServlet?command=SelectProgram&amp;programType=IIS')" type="button">

VBA

Sub login() 'this is working

    Const Url$ = "https://www.mast-technicalservices.com/ecp/index2.jsp"

    Dim UserName As String, Password As String, LoginData As Workbook, elems As Object


    UserName = ThisWorkbook.Sheets("Sheet1").Range("B1")
    Password = ThisWorkbook.Sheets("Sheet1").Range("B2")

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

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

        Dim oLogin As Object, oPassword As Object
        Set oLogin = .document.getElementsByName("ecp_param_userId")(0)
        Set oPassword = .document.getElementsByName("ecp_param_password")(0)

        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit

        ieBusy ie

        Application.Wait (Now + TimeValue("0:00:02"))
        ' After 2sec wait time I need to click the said button to navigate to the next page



    End With

End Sub

Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
            DoEvents
    Loop
End Sub
4

1 回答 1

1

您可以尝试使用CSS 选择器来选择目标元素。然后.fireEvent使用事件名称对所选元素进行方法。


CSS 选择器:

使用选择器选择元素

input[name='reportIIS']

这意味着input带有属性的标签name ="reportIIS"

CSS 选择器

所以,

Dim elem As Object
Set elem = ie.document.querySelector("input[name='reportIIS']")

.fireEvent:

然后触发相关事件:

elem.fireEvent "onclick"

或者

elem.fireEvent "onSelect"

看起来是前者。


有关.fireEvent方法的更多信息:

fireEvent方法:在对象上触发指定事件。

句法:object.fireEvent(bstrEventName, pvarEventObject, pfCancelled)

它有一个布尔返回值,如果它TRUE告诉您事件已成功触发。


.querySelector方法:

Document方法querySelector()返回Element 文档中与指定选择器或选择器组匹配的第一个。如果没有找到匹配项,则返回 null。

语法:element = document.querySelector(selectors); <== 注意“;” 在 VBA 中不使用

于 2018-06-20T06:06:31.153 回答