VBA导航IE javascript链接

VBA navigating IE javascript link

本文关键字:链接 javascript IE 导航 VBA      更新时间:2023-09-26

第一篇。不知道该怎么做。从来没有从网站上检索过数据。可以通过VBA导航到一个带有打开报表选项的页面,但在这一点上,从html到Javascript都可以打开报表。我该如何开始写报告呢?在导航到页面或从表

中拉下数据时没有问题。
<div id='delivery_reports' class='menu' onMouseover='aMenu(this);'>
<table>
<tr>
<td nowrap><a href="javascript:handleHyperlink('finish.do')"
class="mainSubLink">finish Report</a></td>
</tr>

<tr>
<td nowrap><a href="javascript:handleHyperlink('start.do')"
class="mainSubLink">start Report</a></td>
</tr>

<tr>
<td nowrap><a href="javascript:handleHyperlink('control.do')"
class="mainSubLink">control report</a></td>
</tr>

<tr>
<td nowrap><a href="javascript:handleHyperlink('data_start.do')"
class="mainSubLink">data start Report</a></td>
</tr>
</table>
</div>
例如,我如何导航到控制报告?

任何帮助都很感激,谢谢你

你可以使用IE.Navigate方法(假设IE是你的InternetExplorer对象)。比如IE.Navigate "javascript:handleHyperlink('control.do')"

需要注意的是,页面的HTML文档将被重新加载(或者更确切地说,这是我需要使用的一次),因此,在使用导航方法后,页面上引用HTML元素的任何变量都必须重新分配。

一个更完整的例子:

Sub Test()
    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
    Set ie = New InternetExplorer
    ie.Navigate "http://www.yoururl.com"
    'Wait for page to load
    Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    Set doc = ie.document
    'Do whatever manipulation of the HTML document you need
    ie.Navigate "javascript:handleHyperlink('control.do')"
    'Wait for page to load
    Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    'Reassign document
    Set doc = ie.document
    'Further manipulation
End Sub

这就是我发现的基于为我自己的需要编写一些代码的方法。也许还有更好的办法。

附录

你可以通过使用两个方法(我知道)来执行JavaScript函数。一个是execScript,另一个是FireEvent。请记住,IE是您的InternetExplorer对象。

execScript

IE.document.all.Item 
Call IE.document.parentWindow.execScript("handleShowAllLines()", "JavaScript")
此外,由于js代码绑定到一个按钮,你也可以使用FireEvent如下:
IE.Document.GetElementsByTagName("showAllLinesButton").Item(0).FireEvent("onclick")

这个方法假设只有一个名为"showAllLinesButton"的按钮。如果不是这种情况,您必须调整.Item(0)中的索引0,使其成为需要单击的正确按钮的索引。

我没有测试过这两种方法,我不能完全确定一种方法相对于另一种方法的优点/缺点。