Article View: borland.public.cppbuilder.internet.web
Article #1038Re: How to click button on Web Page ?
From: Remy Lebeau (TeamB)
Date: Wed, 14 Sep 2005 20:56
Date: Wed, 14 Sep 2005 20:56
147 lines
5933 bytes
5933 bytes
"Sergey Muschin" <senimu@hotmail.com> wrote in message
news:43285392$1@newsgroups.borland.com...
> IHTMLDocument2 *HTMLDocument = NULL;
> IHTMLInputButtonElement *btn = NULL;
> IHTMLElementCollection * el = NULL;
> LPDISPATCH pBtn = NULL;
At no point are you Release()'ing any of those interfaces. You must do so.
Either do it manually, or else wrap all of your interface pointers with the
TComInterface or CComPtr class so that they are automatically released when
they go out of scope.
> if(hr != S_OK)
Use the SUCCEEDED() and FAILED() macros instead of testing S_OK directly.
> hr = HTMLDocument->get_all(&el);
> if (hr != S_OK) {
> ShowMessage("HTMLDocument->get_links(&el) FAILED");
> }
Why are you retrieving the links collection?
> TVariant vAddress = "Logon", vIndex = 0;
You should type-cast the string literal to WideString:
TVariant vAddress = WideString("Logon");
TVariant vIndex = 0;
I have seen problems with Borland's Variant support where char* string
literals are interpreted as booleans instead of strings.
> now i would like to achive something like btn->Click() to submit the page
I guess you did not look at the IHTMLInputButtonElement documentation yet,
or you would see that all HTML elements have a click() method available via
the IHTMLElement interface.
However, a better approach is to submit the form itself directly instead of
clicking on the button. IHTMLInputButtonElement has a get_form() method
that returns an IHTMLFormElement object. IHTMLFormElement has a submit()
method available, ie:
#include <utilcls.h>
TComInterface<IHTMLFormElement> pForm;
hr = pBtn->get_form(&pForm);
if( SUCCEEDED(hr) )
pForm->submit();
Note that submit() does not trigger the form's OnSubmit event, so if you
need to support that event then you will have to call it manually, ie
(untested):
#include <utilcls.h>
TComInterface<IHTMLFormElement> pForm;
hr = pBtn->get_form(&pForm);
if( SUCCEEDED(hr) )
{
// the onsubmit event handler code can cancel the form's
submission...
VARIANT_BOOL bOkToSubmit = VARIANT_TRUE;
// createEventObject() and fireEvent() requires IE v5.5+ to be
installed...
TComInterface<IHTMLDocument4> HTMLDocument4;
hr = HTMLDocument->QueryInterface(IID_IHTMLDocument4,
(LPVOID*)&HTMLDocument4);
if( SUCCEEDED(hr) )
{
TComInterface<IHTMLElement3> pForm3;
hr = pForm->QueryInterface(IID_IHTMLElement3, (LPVOID*)&pForm3);
if( SUCCEEDED(hr) )
{
TComInterface<IHTMLEventObj> pEvent;
hr = HTMLDocument4->createEventObject(NULL, &pEvent);
if( SUCCEEDED(hr) )
{
TVariant vEventObj = (IDispatch*)(IHTMLEventObj*)pEvent;
hr = pForm->fireEvent(WideString("onsubmit"),
&vEventObj, &bOkToSubmit);
}
}
}
// if IE v5.5+ is not installed, or failed to fire the event, fire
it directly...
if( FAILED(hr) )
{
TVariant vOnSubmit;
hr = pForm->get_onsubmit(&vOnSubmit);
if( SUCCEEDED(hr) && (V_VT(&vOnSubmit) == VT_DISPATCH) )
{
LPDISPATCH pOnSubmit = V_DISPATCH(&vOnSubmit);
if( pOnSubmit )
{
DISPPARAMS params = {0};
TVariant vResult;
hr = pOnSubmit->Invoke(DISPID_VALUE, IID_NULL,
LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, &vResult, NULL, NULL);
if( SUCCEEDED(hr) )
{
// the IHTMLEventObj::returnValue property takes
// precedence over the event handler's return
value...
TComInterface<IHTMLWindow2> HTMLWindow;
hr = HTMLDocument->get_parentWindow(&HTMLWindow);
if( SUCCEEDED(hr) )
{
TComInterface<IHTMLEventObj> pEvent;
hr = HTMLWindow->get_event(&pEvent);
if( SUCCEEDED(hr) )
{
TVariant vReturnValue;
hr = pEvent->get_returnValue(&vReturnValue);
if( SUCCEEDED(hr) && (V_VT(&vReturnValue) ==
VT_BOOL) )
bOkToSubmit = V_BOOL(&vReturnValue);
}
}
if( (bOkToSubmit == VARIANT_TRUE) && (V_VT(&vResult)
== VT_BOOL) )
bOkToSubmit = V_BOOL(&vResult);
}
}
}
}
if( bOkToSubmit == VARIANT_TRUE )
pForm->submit();
}
Gambit
Technical Details
Message-ID:
<43288e83$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.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:
<43285392$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,20ab0c6ae146033c 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: <43285392$1@newsgroups.borland.com> Subject: Re: How to click button on Web Page ? Date: Wed, 14 Sep 2005 13:56:39 -0700 Lines: 149 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: 68.69.17.41 Message-ID: <43288e83$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1126731395 68.69.17.41 (14 Sep 2005 13:56:35 -0700) Path: arc1-ca.pugleaf.net!g2news1.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