🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #460

Re: Help with TCppWebBrowser - Find and click a button

#460
From: Remy Lebeau (TeamB)
Date: Fri, 05 Jan 2007 21:41
121 lines
4593 bytes
"mike" <mfbrowne@earthlink.nnet> wrote in message
news:459ebec4$1@newsgroups.borland.com...

> I would like to programmably be able to click a button on a web
> page loaded into a TCppWebBrowser.

You need to use the DOM intefaces for that.  If the button has a name,
then you can use the IHTMLDocument2::all collection to find it
quickly.  Otherwise, you have to drill down the parent/child hierarchy
manually.  Either way, once you locate the button, you can obtain its
IHTMLElement interface and then call its click() method.

> The web page has a Tree View, I believe, on the left side and the
> button is located within this view.

There is no TreeView control in HTML.

> If I view the source from within the browser the source looks like
below:

The only non-form button I see in that code is named "secure_exit".  I
am assuming that the one you want.  The code to access it would look
like this:

    #include <mshtml.h>
    #include <utilcls.h>

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);

        if( Doc )
        {
            TComInterface<IHTMLElementCollection> All;
            Doc->get_all(&All);

            if( All )
            {
                TComInterface<IDispatch> Disp;
                All->item(TVariant(WideString("secure_exit")),
TVariant(0), &Disp);

                if( Disp )
                {
                    TComInterface<IHTMLElement> Element;
                    Disp->QueryInterface(IID_IHTMLElement,
(LPVOID*)&Element);

                    if( Element )
                        Element->click();
                }
            }
        }
    }

> This portion of the web page may also reside within a frame

That makes a big difference.  You need the IHTMLDocument2 interface
for the specific frame that you are going to search.  Starting with
the top-level Document, you can use its frames collection to access
the IHTMLWindow2 interface of each frame.  You can then use the
IHTMLWindow2::document property to get the IHTMLDocument2 interface.
For example:

    #include <mshtml.h>
    #include <utilcls.h>

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);

        if( Doc )
        {
            TComInterface<IHTMLFramesCollection2> Frames;
            Doc->get_frames(&Frames);

            if( Frames )
            {
                TVariant IndexOrName = WideString("frame name");
                // or:
                // TVariant IndexOrName = SomeIndex;

                TVariant Frame;
                Frames->item(&IndexOrName, &Frame);

                if( (V_VT(&Frame) == VT_DISPATCH) &&
(V_DISPATCH(&Frame) != NULL) )
                {
                    TComInterface<IHTMLWindow2> Window;

V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,
(LPVOID*)&Window);

                    if( Window )
                    {
                        TComInterface<IHTMLDocument2> FrameDoc;
                        Window->get_document(&FrameDoc);

                        if( FrameDoc )
                        {
                            // use FrameDoc as needed ...
                        }
                    }
                }
            }
        }
    }

Refer to the following documentation for more information:

    Interfaces and Scripting Objects

http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/interface.asp


Gambit
Technical Details
Message-ID: <459ec5bf$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <459ebec4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,6ed59181280548e1
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.web
References: <459ebec4$1@newsgroups.borland.com>
Subject: Re: Help with TCppWebBrowser - Find and click a button
Date: Fri, 5 Jan 2007 13:41:11 -0800
Lines: 123
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.34.171
Message-ID: <459ec5bf$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1168033215 75.83.34.171 (5 Jan 2007 13:40:15 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail