简体   繁体   English

VBA导航IE javascript链接

[英]VBA navigating IE javascript link

First post. 第一篇文章。 No idea how to do this. 不知道该怎么做。 Never retrieved data from a website before. 从来没有从网站检索过数据。 Can navigate through VBA to a page with options to open reports, but at this point everything goes from html to Javascript to open reports. 可以通过VBA导航到带有打开报告选项的页面,但是此时一切都从html到Javascript到打开报告。 How would I go about opening the report? 我将如何打开报告? No problems in navigating to the page or pulling down the data from the table 导航到页面或从表中提取数据没有问题

<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>

How would i navigate to the control report for example? 例如,我将如何导航到控制报告?

Any help appreciated, thankyou 任何帮助表示赞赏,谢谢

You can use the IE.Navigate method (assuming here that IE is your InternetExplorer object). 您可以使用IE.Navigate方法(假设IE是您的InternetExplorer对象)。 You would do something like IE.Navigate "javascript:handleHyperlink('control.do')" 您将执行类似IE.Navigate "javascript:handleHyperlink('control.do')"

It's important to note that the page's HTML document will be reloaded (or rather, it was the one time I needed to use this), therefore any variables you have referencing HTML Elements on the page will have to be re-assigned after you use the Navigate method. 请务必注意,页面的HTML文档将被重新加载(或者,这是我需要使用的一次),因此,在您使用了HTML元素后,必须重新分配页面上引用HTML元素的所有变量。导航方法。

A more complete example: 一个更完整的示例:

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

This is just how I've found to do it based on making some code work for my own needs. 这就是我根据使某些代码能够满足自己的需求而找到它的方式。 There may be an even better way. 可能会有更好的方法。

Addendum 附录

You can execute JavaScript functions by using two methods (that I know of). 您可以使用两种方法(我知道)来执行JavaScript函数。 The first is the execScript and the other is FireEvent . 第一个是execScript ,另一个是FireEvent Remember that IE is your InternetExplorer object. 请记住, IE是您的InternetExplorer对象。

execScript EXECSCRIPT

IE.document.all.Item 
Call IE.document.parentWindow.execScript("handleShowAllLines()", "JavaScript")

Additionally, since the js code is tied to a button, you can also use the FireEvent as such: 此外,由于js代码与按钮绑定在一起,因此您还可以按以下方式使用FireEvent

IE.Document.GetElementsByTagName("showAllLinesButton").Item(0).FireEvent("onclick")

This method assumes that there is only one button with the name "showAllLinesButton". 此方法假定只有一个名为“ showAllLinesButton”的按钮。 If that is not the case, you'd have to adjust the index of 0 in the .Item(0) to be the index of the correct button that needs clicked. 如果不是这种情况,则必须将.Item(0)的索引0调整为需要单击的正确按钮的索引。

I have not tested either method, and I am not completely sure of the advantages/drawbacks of one approach over the other. 我没有测试过任何一种方法,也不能完全确定一种方法相对于另一种方法的优点/缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM