VBA 浏览 IE javascript 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25125265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:04:53  来源:igfitidea点击:

VBA navigating IE javascript link

javascriptexcelvbaexcel-vba

提问by jc123456

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. How would I go about opening the report? No problems in navigating to the page or pulling down the data from the table

第一个帖子。不知道如何做到这一点。以前从未从网站检索数据。可以通过 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>

How would i navigate to the control report for example?

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

Any help appreciated, thankyou

任何帮助表示赞赏,谢谢

回答by Soulfire

You can use the IE.Navigatemethod (assuming here that IE is your InternetExplorer object). You would do something like IE.Navigate "javascript:handleHyperlink('control.do')"

您可以使用该IE.Navigate方法(这里假设 IE 是您的 InternetExplorer 对象)。你会做类似的事情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 元素的任何变量都必须在使用后重新分配导航方法。

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). The first is the execScriptand the other is FireEvent. Remember that IEis your InternetExplorerobject.

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

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 FireEventas such:

此外,由于 js 代码绑定到一个按钮,您还可以这样使用FireEvent

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

This method assumes that there is only one button with the name "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.

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

I have not tested either method, and I am not completely sure of the advantages/drawbacks of one approach over the other.

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