Link to home
Start Free TrialLog in
Avatar of PastorDwayne
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::IHTMLElementCollectionPtr pAllElems= pDoc->all;
MSHTML::IHTMLInputTextElementPtr pTextInput= pAllElems->item("editbox_name");
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.
Avatar of Pavlik
Pavlik

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.
Avatar of PastorDwayne

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
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::InternetExplorer));
    MSHTML::IHTMLDocument2Ptr pHTMLDoc;
   
    pIE->Visible = true;
    pIE->Navigate("file://c:/1.html");
    while(pIE->GetBusy())
      Sleep(100);
    pIE->GetDocument()->QueryInterface(&pHTMLDoc);

    MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t("frame1"));
    MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("form1"));
    MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Pavlik
Pavlik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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->item(&_variant_t(0L));
MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("f"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_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.
The problem here is that Yahoo page doesn't have frames. So you should simply get the form from the root document.

MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pDoc->forms->item(&_variant_t("f"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("p"));
pInput->value = "Test string";
Sleep(5000);
pForm->submit();


Pavlik,

This worked great! Thanks very much for your help.