Solved

IHTML question

Posted on 2002-04-19
8
601 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 69
noX challenge 17 112
Unix Command -- Challenging  question 7 88
how to split multiple lines delimiter : 8 97
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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