在我的工作中,我们有一个非常繁琐的过程,我们必须从网络数据库中提取信息并输入到 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
任何人都可以帮助我解决这个问题或者我缺少什么吗?
我对这类事情还很陌生,所以我意识到我可能需要更多关于这个话题的教育。如果有更好的方法,我很高兴知道。在这方面,我使用的是 vbscript,因为我需要用来搜索的数据来自 Access 数据库,我最终将使用 VBA 遍历列表来完成我需要的网络操作。
最佳答案
感谢所有提供帮助的人。然而,由于某种原因,上述方法在这个特定的网页上不起作用。不过,我确实找到了解决方案,并希望发布答案,以防它对其他可能遇到此问题的人有所帮助。
点击请求链接的代码如下。
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
关于javascript - VBScript 单击网页表上调用嵌入式 (?) Javascript 函数的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256486/