setting form element values using ie TCppWebBrowser control
-
This is what I've come up with so far but need to find & add values to the form elements for user input and password input, then submit. I'm using C++Builder6. #include // TComInterfaceHTMLDoc; Browser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&HTMLDoc); //get all the forms TComInterfaceHTMLforms; HTMLDoc->get_forms(&HTMLforms); //Get form 0 since login pages have only one form TComInterfaceHTMLform; //Get User,Password fields & set values TComInterfaceHTMLfield; //Then submit form HTMLform->submit(); Thanks!
-
This is what I've come up with so far but need to find & add values to the form elements for user input and password input, then submit. I'm using C++Builder6. #include // TComInterfaceHTMLDoc; Browser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&HTMLDoc); //get all the forms TComInterfaceHTMLforms; HTMLDoc->get_forms(&HTMLforms); //Get form 0 since login pages have only one form TComInterfaceHTMLform; //Get User,Password fields & set values TComInterfaceHTMLfield; //Then submit form HTMLform->submit(); Thanks!
I hope this Boland C++ Builder 6 code might work..
#include utilcls.h
#include Mshtml.htemplate<class ParentIntf, class ItemIntf>
HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const
WideString &name, ItemIntf** ppIntf)
{
TVariant vName = name;
TVariant vIndex = 0;
TComInterface disp;HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(\_\_uuidof(ItemIntf),(LPVOID\*)ppIntf); return hRes;
}
void __fastcall TForm1::CppWebBrowser1DocumentComplete (TObject*
Sender, LPDISPATCH pDisp, TVariant *URL)
{
if( CppWebBrowser->Document )
{
TComInterface HTMLDoc;
if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(
IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) )
{
TComInterface forms;
if( SUCCEEDED(HTMLDoc->get_forms(&forms)) )
{
TComInterface form;
if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) )
{
TComInterface userid;
TComInterface password;GetHTMLItem(form, "userid", &userid); GetHTMLItem(form, "password", &password); if( userid ) userid->put\_value(WideString("username")); if( password ) password->put\_value(WideString("password")); } } } }
}
I hope this Boland C++ Builder 6 code might work too..
bool TForm1::Login(char* inputname, const AnsiString& text)
{
bool done = false;IHTMLDocument2* HTMLDoc = NULL;
if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&HTMLDoc)))
{
IHTMLElementCollection* pAll = NULL;
if(SUCCEEDED(HTMLDoc->get_all(&pAll)))
{
//Variant name = inputname;
Variant index = 0;IDispatch* pDisp = NULL;
if(SUCCEEDED(pAll->item(name, index, &pDisp)))
{
if ( pDisp )
{
// IHTMLInputFileElement* pInput = NULL; // mshtml.h
IHTMLInputElement* pInput = NULL; // mshtml.h
// IHTMLFormElement* pForm = NULL; // mshtml.hpDisp->QueryInterface(IID\_IHTMLInputElement, (LPVOID\*)&pInput); pDisp->Release(); if(pInput) { pInput->put\_value(WideString(text)); done = true; /\* WideString mybuffer; pInput->get\_value ( &mybuffer ); ShowMessage ( mybuffer ); \*/ pInput->Release(); } }
}
pAll->Release();
}HTMLDoc->Release();
}
return done;
} -
I hope this Boland C++ Builder 6 code might work..
#include utilcls.h
#include Mshtml.htemplate<class ParentIntf, class ItemIntf>
HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const
WideString &name, ItemIntf** ppIntf)
{
TVariant vName = name;
TVariant vIndex = 0;
TComInterface disp;HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(\_\_uuidof(ItemIntf),(LPVOID\*)ppIntf); return hRes;
}
void __fastcall TForm1::CppWebBrowser1DocumentComplete (TObject*
Sender, LPDISPATCH pDisp, TVariant *URL)
{
if( CppWebBrowser->Document )
{
TComInterface HTMLDoc;
if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(
IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) )
{
TComInterface forms;
if( SUCCEEDED(HTMLDoc->get_forms(&forms)) )
{
TComInterface form;
if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) )
{
TComInterface userid;
TComInterface password;GetHTMLItem(form, "userid", &userid); GetHTMLItem(form, "password", &password); if( userid ) userid->put\_value(WideString("username")); if( password ) password->put\_value(WideString("password")); } } } }
}
I hope this Boland C++ Builder 6 code might work too..
bool TForm1::Login(char* inputname, const AnsiString& text)
{
bool done = false;IHTMLDocument2* HTMLDoc = NULL;
if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&HTMLDoc)))
{
IHTMLElementCollection* pAll = NULL;
if(SUCCEEDED(HTMLDoc->get_all(&pAll)))
{
//Variant name = inputname;
Variant index = 0;IDispatch* pDisp = NULL;
if(SUCCEEDED(pAll->item(name, index, &pDisp)))
{
if ( pDisp )
{
// IHTMLInputFileElement* pInput = NULL; // mshtml.h
IHTMLInputElement* pInput = NULL; // mshtml.h
// IHTMLFormElement* pForm = NULL; // mshtml.hpDisp->QueryInterface(IID\_IHTMLInputElement, (LPVOID\*)&pInput); pDisp->Release(); if(pInput) { pInput->put\_value(WideString(text)); done = true; /\* WideString mybuffer; pInput->get\_value ( &mybuffer ); ShowMessage ( mybuffer ); \*/ pInput->Release(); } }
}
pAll->Release();
}HTMLDoc->Release();
}
return done;
}/* Well I got this far I get 3 errors in the BrowserDocumentComplete event: (C++ Error)BrowserFrm.cpp(53): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLFormElement * *)' (C++ Error)BrowserFrm.cpp(58): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' (C++ Error)BrowserFrm.cpp(59): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' */ //I marked the lines with the errors below: //BrowserFrm.cpp #include #pragma hdrstop #include "BrowserFrm.h" #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "SHDocVw_OCX" #pragma resource "*.dfm" TBrowserFrame *BrowserFrame; //--------------------------------------------------------------------------- __fastcall TBrowserFrame::TBrowserFrame(TComponent* Owner) : TFrame(Owner) { } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::Go(AnsiString url) { Browser->Navigate(StringToOleStr(url),0,NULL,NULL,NULL); } //--------------------------------------------------------------------------- template HRESULT __fastcall TBrowserFrame::GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterface disp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform; /*Line 53 error*/ if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) ) { TComInterfaceuserid; TComInterfacepassword;
-
/* Well I got this far I get 3 errors in the BrowserDocumentComplete event: (C++ Error)BrowserFrm.cpp(53): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLFormElement * *)' (C++ Error)BrowserFrm.cpp(58): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' (C++ Error)BrowserFrm.cpp(59): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' */ //I marked the lines with the errors below: //BrowserFrm.cpp #include #pragma hdrstop #include "BrowserFrm.h" #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "SHDocVw_OCX" #pragma resource "*.dfm" TBrowserFrame *BrowserFrame; //--------------------------------------------------------------------------- __fastcall TBrowserFrame::TBrowserFrame(TComponent* Owner) : TFrame(Owner) { } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::Go(AnsiString url) { Browser->Navigate(StringToOleStr(url),0,NULL,NULL,NULL); } //--------------------------------------------------------------------------- template HRESULT __fastcall TBrowserFrame::GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterface disp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform; /*Line 53 error*/ if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) ) { TComInterfaceuserid; TComInterfacepassword;
-
Change this line:
HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf)
To this:
HRESULT __fastcall GetHTMLItem(TComInterface<ParentIntf> &CollectionOrElement, const WideString &name, ItemIntf** ppIntf)
That Worked! It Fills the values but when I submit the form the web browser comes up with a dialog with a caption of: _ASSERTE: and Message of: intf = 0 @ C:\Program Files\Borland\CBuilder6\Include\Vcl\utilcls.h/2916 Press[Y]es to terminate, [N]o to continue and [C]ancel to Debug My code: template HRESULT __fastcall TBrowserFrame::GetHTMLItem(TComInterface &CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterfacedisp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc;; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform;; if( SUCCEEDED(GetHTMLItem(forms, "idpform", &form)) ) { TComInterfaceuserid; TComInterfacepassword; GetHTMLItem(form, "txtUsername", &userid); GetHTMLItem(form, "txtPassword", &password); if( userid ) userid->put_value(WideString(username)); if( password ) password->put_value(WideString(pass)); //form->submit(); } } } } } //---------------------------------------------------------------------------
-
That Worked! It Fills the values but when I submit the form the web browser comes up with a dialog with a caption of: _ASSERTE: and Message of: intf = 0 @ C:\Program Files\Borland\CBuilder6\Include\Vcl\utilcls.h/2916 Press[Y]es to terminate, [N]o to continue and [C]ancel to Debug My code: template HRESULT __fastcall TBrowserFrame::GetHTMLItem(TComInterface &CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterfacedisp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc;; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform;; if( SUCCEEDED(GetHTMLItem(forms, "idpform", &form)) ) { TComInterfaceuserid; TComInterfacepassword; GetHTMLItem(form, "txtUsername", &userid); GetHTMLItem(form, "txtPassword", &password); if( userid ) userid->put_value(WideString(username)); if( password ) password->put_value(WideString(pass)); //form->submit(); } } } } } //---------------------------------------------------------------------------
What you are likely seeing is a call to
MessageBox()
before the exception is thrown. To turn that off, you can go into the Project Options and addNO_PROMPT_ON_HRCHECK_FAILURE
to the Conditionals list. The exception will then be thrown without any prompting. If you are still using theTComInterface
wrapper, the '->' operator validates whether the interface pointer is available or not. If it is not, an ASSERT is thrown. To disable the prompt on that error, you can defineNO_PROMPT_ON_ASSERTE_FAILURE
in the Project Options. If you implement a try..catch block, it will catch errors normally without messagebox prompt interruptions. (sample try catch)try {
buf = new char[512];
if( buf == 0 ) throw "Memory allocation failure!";
}
catch( char * str ) {
cout << "Exception raised: " << str << '\n';
} -
What you are likely seeing is a call to
MessageBox()
before the exception is thrown. To turn that off, you can go into the Project Options and addNO_PROMPT_ON_HRCHECK_FAILURE
to the Conditionals list. The exception will then be thrown without any prompting. If you are still using theTComInterface
wrapper, the '->' operator validates whether the interface pointer is available or not. If it is not, an ASSERT is thrown. To disable the prompt on that error, you can defineNO_PROMPT_ON_ASSERTE_FAILURE
in the Project Options. If you implement a try..catch block, it will catch errors normally without messagebox prompt interruptions. (sample try catch)try {
buf = new char[512];
if( buf == 0 ) throw "Memory allocation failure!";
}
catch( char * str ) {
cout << "Exception raised: " << str << '\n';
}Thankyou very much...every thing worked I added NO_PROMPT_ON_HRCHECK_FAILURE to the Conditionals list. and inserted a try/catch block around my code in the document complete event. Now if every login page had the same names for their form & input names!