Solved

IHTML question

Posted on 2002-04-19
8
608 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:PastorDwayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955122
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.
0
 

Author Comment

by:PastorDwayne
ID: 6959433
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
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960599
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PastorDwayne
ID: 6960954
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.
0
 
LVL 1

Accepted Solution

by:
Pavlik earned 250 total points
ID: 6961137
If you don't have frame names, then just select frame by its index.

Remove attribute name from frame tag from 1.html:
<frame src="file://c:/2.html">
And change:
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t("frame1"));
to:
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t(0L));


That's all you need.

> 1.  I would like to incorporate the ActiveX WebBrowser control inside a dialog box rather than IE.
Let's connect it to your code:
=====================================
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument(); // This is what you have in the first line

//And this is the remaining
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pDoc->frames->item(&_variant_t(0L));
MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("form1"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("i1"));
pInput->value = "Test string";
======================================

Just you can get IWebBrowser2 interface from IE and from WebBrowser control. And it will always have document inside.
0
 

Author Comment

by:PastorDwayne
ID: 6962543
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.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6963317
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();


0
 

Author Comment

by:PastorDwayne
ID: 6973182
Pavlik,

This worked great! Thanks very much for your help.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question