如何使用 VBA 访问网页(IHTMLDocument?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5786271/
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
How Can I Use VBA To Access A WebPage (IHTMLDocument?)
提问by Jason
Using the following code, I can open up an Internet Explorer window and navigate to the website. I would like to go further by being able to have a code click through one of the links (such as "Boxing/UFC").
使用以下代码,我可以打开 Internet Explorer 窗口并导航到该网站。我希望能够通过其中一个链接(例如“拳击/UFC”)点击代码来更进一步。
Sub Run()
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate2 "http://www.bet365.com"
Do Until .ReadyState = 4: DoEvents: Loop
.Quit
End With
Because that link leads to a Javascript onclick
event (onclick="gS(1020,9);return false;"
) can you suggest code I could use that would enable VBA to click through that link without the user's interference once the VBA macro is run?
因为该链接会导致 Javascriptonclick
事件 ( onclick="gS(1020,9);return false;"
),您能否建议我可以使用的代码,使 VBA 可以在运行 VBA 宏后单击该链接而无需用户干预?
回答by Tomalak
In Internet Explorer, you can simply click a link via the DOM API.
在 Internet Explorer 中,您只需通过 DOM API 单击链接即可。
Retrieve the DOM node (the A
) you are interested in, and call click()
on it. This will also fire all associated event handlers.
检索A
您感兴趣的 DOM 节点 (the ),并调用click()
它。这也将触发所有关联的事件处理程序。
EDIT: In your case, with VBA, do something like this (untested, I don't have VBA here)
编辑:在你的情况下,使用 VBA,做这样的事情(未经测试,我这里没有 VBA)
Dim sideNav As IHTMLElement
Dim navLinks As IHTMLElementCollection
Dim currLink As IHTMLElement
Set sideNav = IE.document.getElementByID("sln")
Set navLinks = sideNav.all.tags("A")
For Each currLink In navLinks
If Trim(currLink.innerText) = "Boxing/UFC" Then
currLink.Click
End If
Next currLink
回答by jusathr
You would need to query the html document, and retrieve the collection. Once you retrieve the collection you would need to iterate through the elements and click it.
您需要查询 html 文档并检索集合。检索集合后,您需要遍历元素并单击它。
I am pasting a snippet of the code here (click here for the entire code). This is in c++, but i am assuming it must be the same for VB too.
我在这里粘贴了一段代码(点击这里查看整个代码)。这是在 c++ 中,但我假设它对于 VB 也必须相同。
IHTMLElementCollection *spCollectEmbed;
spDocument->get_links(&spCollectEmbed);
if(spCollectEmbed)
{
// get all the links
long lLen;
spCollectEmbed->get_length(&lLen);
for (long i = 0; i < lLen; i++)
{
IDispatch *ppvdispOption;
IHTMLElement *interfaceHTMLElement;
VARIANT index;
index.vt = VT_I4;
index.lVal = i;
// get the item from the document
HRESULT hResult = spCollectEmbed->item(index, index, &ppvdispOption);
if(SUCCEEDED(hResult))
{
// query for the element
hResult = ppvdispOption->QueryInterface( IID_IHTMLElement,
(void **) &interfaceHTMLElement);
if(SUCCEEDED(hResult))
{
BSTR innerhtml;
interfaceHTMLElement->get_innerHTML(&innerhtml);
// click the links
interfaceHTMLElement->click();
Sleep(2000);
}