VBScript以单击网页表上的链接,该链接调用嵌入式(?)Javascript函数 [英] VBScript to Click Link on a Web Page Table That Calls Embedded (?) Javascript Function

查看:99
本文介绍了VBScript以单击网页表上的链接,该链接调用嵌入式(?)Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我们有一个非常繁琐的过程,必须从网络数据库中提取信息并输入到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函数来设置光标在表单上的位置,单击相应的复选框,然后提交表单,所有这些都将带您进入下一个屏幕. /p>

我可以设置光标位置:

 .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

任何人都可以协助我实现目标或我所缺少的吗?

我几乎是这种类型的婴儿,所以我意识到我可能需要接受更多有关该主题的教育.如果有更好的方法,我很高兴知道这一点.在这方面,我正在使用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

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.

解决方案

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

这篇关于VBScript以单击网页表上的链接,该链接调用嵌入式(?)Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆