Article View: borland.public.cppbuilder.internet.web
Article #479Re: Help with TCppWebBrowser - Find and click a button
From: Hans Galema
Date: Mon, 08 Jan 2007 12:14
Date: Mon, 08 Jan 2007 12:14
123 lines
4067 bytes
4067 bytes
Hans Galema wrote:
> ... So you should make the code recursive.
In order to do that start at the begin. I first took Remy's code to
click a button if the webbrowsercontrol contains a document without
a frameset. I first made a function of it. ( I keep functions concerning
T(Cpp)WebBrowser in a separate file which is easy added to a project
with the browsercomponent).
To click a certain button then only a few lines are needed. In your
case:
AnsiString ButtonName = "secure_exit" ;
if ( ! WBClickButton ( WebBrowser1, ButtonName) )
ShowMessage ( ButtonName + "\n\nbutton not clicked");
Here is the function:
bool __fastcall WBClickButton ( TWebBrowser *WebBrowser, AnsiString ButtonName )
{
bool done = false;
if( WebBrowser->Document )
{
TComInterface<IHTMLDocument2> Doc;
WebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&Doc);
if( Doc )
{
TComInterface<IHTMLElementCollection> All;
Doc->get_all(&All);
if( All )
{
TComInterface<IDispatch> Disp;
All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);
if( Disp )
{
TComInterface<IHTMLElement> Element;
Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);
if( Element )
{
Element->click();
done = true;
}
}
}
}
}
return done;
}
Then realising that traversing frames we need a function that finds
a button in a IHTMLDocument2 I made another version which puts all
the code in two functions:
bool __fastcall WBClickButton2 ( TWebBrowser *WebBrowser, AnsiString ButtonName )
{
bool clicked = false;
if( WebBrowser->Document )
{
TComInterface<IHTMLDocument2> Doc;
WebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&Doc);
if( Doc )
{
clicked = DocClickButton2 ( Doc, ButtonName ); // see code below
}
}
return clicked;
}
bool __fastcall DocClickButton2 ( TComInterface<IHTMLDocument2> Doc, AnsiString ButtonName )
{
bool clicked = false;
TComInterface<IHTMLElementCollection> All;
Doc->get_all(&All);
if( All )
{
TComInterface<IDispatch> Disp;
All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);
if( Disp )
{
TComInterface<IHTMLElement> Element;
Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);
if( Element )
{
Element->click();
clicked = true;
}
}
}
return clicked;
}
AnsiString ButtonName = "secure_exit" ;
if ( ! WBClickButton2 ( WebBrowser1, ButtonName) )
ShowMessage ( ButtonName + "\n\nbutton not clicked");
Now we can use DocClickButton2() also on the documents for every frame when
traversing Frames.
That has to be done yet.
Hans.
Technical Details
Message-ID:
<45a235ec$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<459ebec4$1@newsgroups.borland.com> <459ec5bf$1@newsgroups.borland.com> <459fbc29$2@newsgroups.borland.com> <45a02189$1@newsgroups.borland.com> <45a02ab5$1@newsgroups.borland.com> <45a0c58e$1@newsgroups.borland.com> <45a0fe8a$1@newsgroups.borland.com> <45a10643$1@newsgroups.borland.com> <45a226d9@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 Date: Mon, 08 Jan 2007 13:14:01 +0100 From: Hans Galema <notused@notused.nl> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.web Subject: Re: Help with TCppWebBrowser - Find and click a button References: <459ebec4$1@newsgroups.borland.com> <459ec5bf$1@newsgroups.borland.com> <459fbc29$2@newsgroups.borland.com> <45a02189$1@newsgroups.borland.com> <45a02ab5$1@newsgroups.borland.com> <45a0c58e$1@newsgroups.borland.com> <45a0fe8a$1@newsgroups.borland.com> <45a10643$1@newsgroups.borland.com> <45a226d9@newsgroups.borland.com> In-Reply-To: <45a226d9@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 145.98.201.248 Message-ID: <45a235ec$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1168258540 145.98.201.248 (8 Jan 2007 04:15:40 -0800) Lines: 123 Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail