VBScript to Click Link on a Web Page Table That Ca

2019-09-11 04:45发布

问题:

At my work we have a very tedious process where we have to pull information from a web database and enter into a word document. Right now, we have no way to query the database and must look up each individual record at a time with many clicks to navigate the web form (there can be over a 100 to look up). I am writing a vbscript to click through the webpages to get the information I need, but am stuck on certain part.

I have the following code to get to the page I need, but when I reach this page, there is only links to click and I cannot understand how to programmatically "click the link" through the vbscript, since, from what I can see, the link calls embedded javascript functions within the website.

Dim objWshShell,IE

Set objWshShell = Wscript.CreateObject("Wscript.Shell")
Set IE = CreateObject("InternetExplorer.Application")

objWshShell.AppActivate IE

With IE

  'set browser to view
  .Visible = True

  'goes straight to needed screen
   .Navigate "https://test/Default.aspx"

  'Wait for Browser
  Do While .Busy or .ReadyState <> 4: WScript.Sleep 100: Loop

 Dim oDoc
 Set oDoc = .Document

 With oDoc

  'pass through first screen 
  '*** this screen has links too, but there is also an input box that you can use, so it's easy to get through
  .getElementsByName("in_2515_60").Item(0).Value = 5
  .Forms(0).Submit()

 'page down on next screen
  For each item in .Forms(0)

    If Instr(1,item.name, "[pagedn]") Then 
             item.click()
             Exit For
     End If

  Next

Here is where I am stuck in "clicking the link" or "submitting the form"

For reference, the HTML behind the link is the following:

<td style="color: black; line-height: 20px; font-weight: bold;">
      <INPUT type=hidden name=in_1191_1>
      <a class="HATSLINK" style="color: black; line-height: 20px; font-weight: bold;" href="javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm')">

From this, I see that when you click the link, it calls 3 javascript functions to set the cursor position on the form, to click the appropriate checkbox and then submit the form, all to take you to the next screen.

I am able to set the cursor position:

 .getElementsByName("CURSORPOSITION").Item(0).value = 1191

and mark the appropriate checkbox

  For each item in .Forms(0)

    If Instr(1,item.name, "in_1191_1") Then 

        item.checked = TRUE
        Exit For

    End If

  Next

But I cannot get the form to submit to take me to the next page. I have tried all the following (and more):

  For each item in .Forms(0)

    If Instr(1,item.name, "[enter]") Then item.click() 'click the Enter (or OK) button

  Next

 objWshShell.SendKeys "{enter}" 'this is a mess and just produces copies of the IE page until I stop it manually

.Forms(0).Submit 'does nothing

Can anyone assist me in how to pull this off or what I am missing?

I am pretty much a baby at this type of stuff, so I realize I may need more education on the topic. If there is a better way to go, I am happy to know about it. In that regard, I am using vbscript, because the data I need to use to search comes out of an Access database and I will eventually loop through the list with VBA to complete the actions on the web that I need.

回答1:

Thanks to everyone who helped out. However, for some reason, the above methods didn't work out in this particular webpage. I did, however, find a solution and wanted to post the answer in case it helps anyone else who may be stuck with this issue.

The code to click the requested link is below.

Option Explicit

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Sub Routine()

Dim IE as Object
Set IE = GetIE

With IE

    .Visible = True
    .Navigate "https://myURL/Default.aspx"

    IEWait

    Dim oDoc as Object, sLink as String

    Set oDoc = .document
    sLInk = "in_1191_1"

    IECLickLink oDoc, sLink

End Sub

Sub IEClickLink(doc As Object, sLink As String)
'assumes IE is loaded as InternetExplorer.Application") with URL loaded
'assumes doc is declared as IE.document

With doc

    Dim oLink As Object
    For Each oLink In .Links

        If InStr(1, oLink.href, sLink) Then 'looks for particular text in href attribute then clicks that particular link
            oLink.Click
            Exit For
        End If
    Next

End With

End Sub

Function GetIE() As Object
  On Error Resume Next
  Set GetIE = CreateObject("InternetExplorer.Application")
End Function

Sub IEWait()
'assumes IE is loaded as InternetExplorer.Application

With IE
    Do While .Busy Or .ReadyState <> 4: Sleep (100): Loop
End With

End Sub


回答2:

You may also have been able to use a CSS selector of

a[href=javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm')]

This literally says get the a tag element with href attribute whose value is javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm'

It would have been applied as:

oDoc.querySelector("a[href=javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm')]").Click


回答3:

You can always use the execScript() function to call a JavaScript function:

IE.document.parentWindow.execScript "ms('[enter]','HATSForm');", "javascript"