Board index » cppbuilder » Clicking an HTMLInputButton with C++Builder

Clicking an HTMLInputButton with C++Builder


2005-02-26 04:47:10 AM
cppbuilder63
I am writing an app that prefills applications on the web with information
stored in my software. I now understand that I must use the Microsoft DOM
to achieve my goals.
Now I am trying to get my app to click a submit button on a form after I
have prefilled the data. The HTML for the button looks like...
<TD align=right vAlign=top><input type=image alt=Submit height=17
src="images/button_submit.gif"
width=42 onClick="submitForm();"></TD>
And my code looks like this (with apologies to Remy):
void __fastcall TMain::PushButton(void)
{
TComInterface<IHTMLDocument2>Doc;
Browser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&Doc);
if( Doc )
{
TComInterface<IHTMLElementCollection>All;
Doc->get_all(&All);
if( All )
{
long count = 0;
All->get_length(&count);
for(long x = 0; x < count; ++x)
{
TVariant item = x;
TVariant index = 0;
TComInterface<IDispatch>Disp;
All->item(item, index, &Disp);
if( Disp )
{
TComInterface<IHTMLInputButtonElement>ButtonInput;
// I've also tried HTMLButtonElement here..
Disp->QueryInterface(IID_IHTMLInputButtonElement,(LPVOID*)&ButtonInput);
if( ButtonInput )
{ShowMessage("Button Found");
// Code goes here to make sure Button is of type
Submit
TComInterface<IHTMLElement>Element;
Disp->QueryInterface(IID_IHTMLElement,(LPVOID*)&Element);
// HTMLInputButtonElement has no click() method so I have to revert
// back to HTMLElement???
if(Element)
{Element->click();}
}
}
}
}
}
}
For some reason the button is never found (the ShowMessage never fires). Am
I using the wrong element?
Thanks for looking.
Mitch McInelly
 
 

Re:Clicking an HTMLInputButton with C++Builder

Mitch McInelly wrote:
Quote
<TD align=right vAlign=top><input type=image alt=Submit height=17
src="images/button_submit.gif"
TComInterface<IHTMLInputButtonElement>ButtonInput;
But you defined your input element of type image. Not of type button.
So I think you should use <IHTMLInputImageElement>. If there is.
Hans.
 

Re:Clicking an HTMLInputButton with C++Builder

"Mitch McInelly" < XXXX@XXXXX.COM >wrote in message
Quote
Now I am trying to get my app to click a submit button on a
form after I have prefilled the data.
<snip>
For some reason the button is never found (the ShowMessage
never fires). Am I using the wrong element?
Yes. Your form is using an image, not a button, so you should be looking
for IHTMLInputImage rather than IHTMLInputButtonElement.
Rathr than trying to click the submit button, you should instead get the
IHTMLFormElement interface for the form itself and call its submit() method.
For example:
void __fastcall TMain::SubmitForm(void)
{
TComInterface<IHTMLDocument2>Doc;
Browser->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);
if( Doc )
{
TComInterface<IHTMLElementCollection>Forms;
Doc->get_forms(&Forms);
if( Forms )
{
long count = 0;
Forms->get_length(&count);
for(long x = 0; x < count; ++x)
{
TVariant item = x;
TVariant index = 0;
TComInterface<IDispatch>Disp;
Forms->item(item, index, &Disp);
if( Disp )
{
TComInterface<IHTMLFormElement>Form;
Disp->QueryInterface(IID_IHTMLFormElement,
(LPVOID*)&Form);
if( Form )
{
ShowMessage("Form Found");
Form->submit();
break;
}
}
}
}
}
If the page has multiple forms, it is your own responsibility to make sure
that you are submitting the proper form. If the forms have names assigned,
you can specify the desired name rather than an index when calling
IHTMLElementCollection::item().
Gambit
 

{smallsort}

Re:Clicking an HTMLInputButton with C++Builder

Again, thank you. It works perfectly!
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Mitch McInelly" < XXXX@XXXXX.COM >wrote in message
news:421f8f60$ XXXX@XXXXX.COM ...

>Now I am trying to get my app to click a submit button on a
>form after I have prefilled the data.
<snip>
>For some reason the button is never found (the ShowMessage
>never fires). Am I using the wrong element?

Yes. Your form is using an image, not a button, so you should be looking
for IHTMLInputImage rather than IHTMLInputButtonElement.

Rathr than trying to click the submit button, you should instead get the
IHTMLFormElement interface for the form itself and call its submit()
method.
For example:

void __fastcall TMain::SubmitForm(void)
{
TComInterface<IHTMLDocument2>Doc;
Browser->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);
if( Doc )
{
TComInterface<IHTMLElementCollection>Forms;
Doc->get_forms(&Forms);
if( Forms )
{
long count = 0;
Forms->get_length(&count);

for(long x = 0; x < count; ++x)
{
TVariant item = x;
TVariant index = 0;
TComInterface<IDispatch>Disp;

Forms->item(item, index, &Disp);
if( Disp )
{
TComInterface<IHTMLFormElement>Form;
Disp->QueryInterface(IID_IHTMLFormElement,
(LPVOID*)&Form);
if( Form )
{
ShowMessage("Form Found");
Form->submit();
break;
}
}
}
}
}

If the page has multiple forms, it is your own responsibility to make sure
that you are submitting the proper form. If the forms have names
assigned,
you can specify the desired name rather than an index when calling
IHTMLElementCollection::item().


Gambit


 

Re:Clicking an HTMLInputButton with C++Builder

Yes, of course. Just cause it looks like a button doesn't mean it's a
button. Very stupid of me.
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Mitch McInelly wrote:

><TD align=right vAlign=top><input type=image alt=Submit height=17
>src="images/button_submit.gif"

>TComInterface<IHTMLInputButtonElement>
>ButtonInput;

But you defined your input element of type image. Not of type button.

So I think you should use <IHTMLInputImageElement>. If there is.

Hans.