VBA导航IE javascript链接

jc1*_*456 2 javascript excel vba excel-vba

第一篇文章。不知道该怎么做。从来没有从网站检索过数据。可以通过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>
Run Code Online (Sandbox Code Playgroud)

例如,我将如何导航到控制报告?

任何帮助表示赞赏,谢谢

Sou*_*ire 5

您可以使用该IE.Navigate方法(假设IE是您的InternetExplorer对象)。你会做类似的事情IE.Navigate "javascript:handleHyperlink('control.do')"

请务必注意,页面的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
Run Code Online (Sandbox Code Playgroud)

这就是我根据使某些代码可以满足自己的需求而找到它的方式。可能会有更好的方法。

附录

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

exec脚本

IE.document.all.Item 
Call IE.document.parentWindow.execScript("handleShowAllLines()", "JavaScript")
Run Code Online (Sandbox Code Playgroud)

此外,由于js代码与按钮绑定在一起,因此您还可以按以下方式使用FireEvent

IE.Document.GetElementsByTagName("showAllLinesButton").Item(0).FireEvent("onclick")
Run Code Online (Sandbox Code Playgroud)

此方法假定只有一个名为“ showAllLinesButton”的按钮。如果不是这种情况,则必须将中的索引0调整为.Item(0)需要单击的正确按钮的索引。

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