PastorDwayne
asked on
IHTML question
Good-day,
I am writing a program in VC 6.0 that will automate input of text on a web page text box. I am using the CWebBrowser2 control and here is the code that I am using:
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument(); MSHTML::IHTMLElementCollec tionPtr pAllElems= pDoc->all;
MSHTML::IHTMLInputTextElem entPtr pTextInput= pAllElems->item("editbox_n ame");
pTextInput->value = "some text";
This code is working fine; my problem is that I have encountered a web page that has mulitiple frames, and I need to specify which frame to select before I can run this.
Any ideas?
Thanks very much.
I am writing a program in VC 6.0 that will automate input of text on a web page text box. I am using the CWebBrowser2 control and here is the code that I am using:
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument(); MSHTML::IHTMLElementCollec
MSHTML::IHTMLInputTextElem
pTextInput->value = "some text";
This code is working fine; my problem is that I have encountered a web page that has mulitiple frames, and I need to specify which frame to select before I can run this.
Any ideas?
Thanks very much.
You should get the frame from IHTMLDocument2.frames.item ("frame name"), cast it to IHTMLWindow2 and get document from it. And after that get elements of this document and do with them whatever you want.
ASKER
Could you give me an example as to how to use this. Lets say that the form name that I am trying to access is named "form1"
Thanks
Thanks
Ok. I have 3 files:
1.html - contains frameset
2.html - contains form
IEManip.cpp - does all the work with them.
Here they are:
1.html
-------------------------- ---------- --------
<html>
<frameset>
<frame name="frame1" src="file://c:/2.html">
</frame>
</frameset>
</html>
========================== ========== ========
2.html
-------------------------- ---------- --------
<html>
<body>
<form name="form1" action="about:blank">
<input name="i1" type="text">
</form>
</body>
</html>
========================== ========== ========
IEManip.cpp
-------------------------- ---------- --------
#import <shdocvw.dll>
#import <mshtml.tlb>
int main(int argc, char* argv[])
{
CoInitialize(0);
{
SHDocVw::IWebBrowser2Ptr pIE(__uuidof(SHDocVw::Inte rnetExplor er));
MSHTML::IHTMLDocument2Ptr pHTMLDoc;
pIE->Visible = true;
pIE->Navigate("file://c:/1 .html");
while(pIE->GetBusy())
Sleep(100);
pIE->GetDocument()->QueryI nterface(& pHTMLDoc);
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frame s->item(&_ variant_t( "frame1")) ;
MSHTML::IHTMLFormElementPt r pForm = (IUnknown*)pHTMLWnd->docum ent->forms ->item(&_v ariant_t(" form1"));
MSHTML::IHTMLInputElementP tr pInput = (IUnknown*)pForm->item(&_v ariant_t(" i1"));
pInput->value = "Test string";
}
CoUninitialize();
return 0;
}
========================== ========== ========
You should put 1.html and 2.html to the root of disk C.
Let me know if you have more questions about this.
1.html - contains frameset
2.html - contains form
IEManip.cpp - does all the work with them.
Here they are:
1.html
--------------------------
<html>
<frameset>
<frame name="frame1" src="file://c:/2.html">
</frame>
</frameset>
</html>
==========================
2.html
--------------------------
<html>
<body>
<form name="form1" action="about:blank">
<input name="i1" type="text">
</form>
</body>
</html>
==========================
IEManip.cpp
--------------------------
#import <shdocvw.dll>
#import <mshtml.tlb>
int main(int argc, char* argv[])
{
CoInitialize(0);
{
SHDocVw::IWebBrowser2Ptr pIE(__uuidof(SHDocVw::Inte
MSHTML::IHTMLDocument2Ptr pHTMLDoc;
pIE->Visible = true;
pIE->Navigate("file://c:/1
while(pIE->GetBusy())
Sleep(100);
pIE->GetDocument()->QueryI
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frame
MSHTML::IHTMLFormElementPt
MSHTML::IHTMLInputElementP
pInput->value = "Test string";
}
CoUninitialize();
return 0;
}
==========================
You should put 1.html and 2.html to the root of disk C.
Let me know if you have more questions about this.
ASKER
This code looks great... just a few questions about it.
1. I would like to incorporate the ActiveX WebBrowser control inside a dialog box rather than IE.
2. I searched the HTML source code for this web page that I am trying to access, and there doesn't seem to be a <frame name> tag anywhere. Is there a default value I can use for this?
Thanks.
1. I would like to incorporate the ActiveX WebBrowser control inside a dialog box rather than IE.
2. I searched the HTML source code for this web page that I am trying to access, and there doesn't seem to be a <frame name> tag anywhere. Is there a default value I can use for this?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can't seem to get this code to work. For example, I load the www.yahoo.com page in my WebBrowser and have a button that is attatched to this code:
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument();
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pDoc->frames->i tem(&_vari ant_t(0L)) ;
MSHTML::IHTMLFormElementPt r pForm = (IUnknown*)pHTMLWnd->docum ent->forms ->item(&_v ariant_t(" f"));
MSHTML::IHTMLInputElementP tr pInput = (IUnknown*)pForm->item(&_v ariant_t(" p"));
pInput->value = "Test string";
The yahoo page form name seems to be "f" and the input edit box is "p". But when I try to run this, the program crashes.
Any ideas?
thanks.
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument();
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pDoc->frames->i
MSHTML::IHTMLFormElementPt
MSHTML::IHTMLInputElementP
pInput->value = "Test string";
The yahoo page form name seems to be "f" and the input edit box is "p". But when I try to run this, the program crashes.
Any ideas?
thanks.
The problem here is that Yahoo page doesn't have frames. So you should simply get the form from the root document.
MSHTML::IHTMLFormElementPt r pForm = (IUnknown*)pDoc->forms->it em(&_varia nt_t("f")) ;
MSHTML::IHTMLInputElementP tr pInput = (IUnknown*)pForm->item(&_v ariant_t(" p"));
pInput->value = "Test string";
Sleep(5000);
pForm->submit();
MSHTML::IHTMLFormElementPt
MSHTML::IHTMLInputElementP
pInput->value = "Test string";
Sleep(5000);
pForm->submit();
ASKER
Pavlik,
This worked great! Thanks very much for your help.
This worked great! Thanks very much for your help.