【发布时间】:2018-07-03 00:58:39
【问题描述】:
在我的工作中,我们有一个非常繁琐的过程,我们必须从 Web 数据库中提取信息并输入 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 遍历列表以完成我需要的网络操作。
【问题讨论】:
-
试试这个VBS代码
oDoc.parentWindow.ms("[enter]", "HATSForm") -
感谢@omegastripes 当我进入我的.vbs 文件时,我得到了错误
Cannot use parentheses when calling a Sub我明白为什么会出现这个错误,但不知道如何解决它?有什么想法吗? -
哦,我的错!
oDoc.parentWindow.ms "[enter]", "HATSForm". -
@omegastripes - 是的,我也试过了。代码执行,但链接没有被触发,页面保持不变。还有其他想法吗?
标签: javascript html internet-explorer vbscript