简体   繁体   English

如何使用VBA访问WebPage(IHTMLDocument?)

[英]How Can I Use VBA To Access A WebPage (IHTMLDocument?)

Using the following code, I can open up an Internet Explorer window and navigate to the website. 使用以下代码,我可以打开Internet Explorer窗口并导航到该网站。 I would like to go further by being able to have a code click through one of the links (such as "Boxing/UFC"). 我想进一步通过其中一个链接(例如“Boxing / 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? 因为该链接导致Javascript onclick事件( onclick="gS(1020,9);return false;" )你可以建议我可以使用的代码,这将使VBA能够在没有用户干扰的情况下点击该链接,一旦VBA宏是跑?

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. 检索您感兴趣的DOM节点( A ),并在其上调用click() This will also fire all associated event handlers. 这也将触发所有相关的事件处理程序。


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

You would need to query the html document, and retrieve the collection. 您需要查询html文档,并检索该集合。 Once you retrieve the collection you would need to iterate through the elements and click it. 检索集合后,您需要遍历元素并单击它。

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);
        }

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

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