0

Hi I'm trying to figure out a way to call a java script mapped to a button using VBA as part of the web page data entry automation. Following is the code from the site and also the code that I'm working on.

Browser code :

<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
Community
  • 1
  • 1
SEEDS Support
  • 33
  • 1
  • 1
  • 6

1 Answers1

1

You might try using a CSS selector to select the target element. Then .fireEvent method on selected element using the event name.


CSS selector:

Select the element with selector

input[name='reportIIS']

Which means input tag with attribute name ="reportIIS"

CSS selector

So,

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

.fireEvent:

Then trigger the associated event with:

elem.fireEvent "onclick"

or

elem.fireEvent "onSelect"

It looks like it is the former.


More info on .fireEvent method:

fireEvent method: Fires a specified event on the object.

Syntax: object.fireEvent(bstrEventName, pvarEventObject, pfCancelled)

It has a boolean return value which if TRUE tells you the event was successfully fired.


.querySelector method:

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.

Syntax: element = document.querySelector(selectors); <== note the ";" is not used in VBA

QHarr
  • 83,427
  • 12
  • 54
  • 101