VBScript点击链接在一个网页表调用嵌入(?)Javascript函数

VBScript to Click Link on a Web Page Table That Calls Embedded (?) Javascript Function

本文关键字:调用 页表 函数 Javascript 网页 一个 链接 VBScript      更新时间:2023-09-26

在我的工作中,我们有一个非常繁琐的过程,我们必须从网络数据库中提取信息并输入word文档。现在,我们没有办法查询数据库,必须查找每一个单独的记录在同一时间与许多点击导航的web表单(可以有超过100查找)。我正在写一个vbscript通过点击网页来获得我需要的信息,但我被困在某些部分。

我有以下代码来获得我需要的页面,但是当我到达这个页面时,只有链接可以点击,我无法理解如何通过vbscript以编程方式"点击链接",因为,从我所看到的,链接在网站内调用嵌入的javascript函数。

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

这里是我卡在"点击链接"或"提交表单"

作为参考,链接后面的HTML如下:

<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')">

从这里,我看到当你点击链接时,它调用3个javascript函数来设置光标在表单上的位置,点击适当的复选框,然后提交表单,所有这些都把你带到下一个屏幕。

我可以设置光标的位置:

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

并标记相应的复选框

  For each item in .Forms(0)
    If Instr(1,item.name, "in_1191_1") Then 
        item.checked = TRUE
        Exit For
    End If
  Next

但是我不能让提交的表单带我到下一页。我试过以下所有方法(甚至更多):

  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

谁能帮助我在如何拉这个off或我错过了什么?

在这方面我还是个新手,所以我意识到我可能需要更多的教育。如果有更好的方法,我很乐意知道。在这方面,我使用vbscript,因为我需要用来搜索的数据来自Access数据库,我最终将通过VBA循环列表来完成我需要的web上的操作。

感谢所有帮助我的人。然而,由于某些原因,上述方法在这个特定的网页上不起作用。然而,我确实找到了一个解决方案,并想把答案贴出来,以防它能帮助到其他可能被这个问题困住的人。

点击所请求链接的代码如下。

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

您也可以使用CSS选择器

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

它的意思是获取带有href属性值为javascript:setCursorPosition(1191, 'HATSForm');checkInput2('in_1191_1', '1','hidden');ms('[enter]', 'HATSForm'的a标签元素

它将被应用为:

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

您总是可以使用execScript()函数来调用JavaScript函数:

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