Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

IHTML submit page

Good-day,

I am writing an MFC C++ application that interacts with a specific web page.  I would like to create a dialog button in MFC that will 'click' on the 'submit' button on the web page.  I am using Microsoft's activeX Web control to load the page.

I am able to click on other button type controls on the web page using IHTMLElementPtr::click, but I could not access the web page's 'submit'or 'go' buttons. They don't seem to have an ID associated with them.

If you could give and example by demonstrating the code that would capturing the 'search' button on the 'Yahoo' page, that would be great...
0
PastorDwayne
Asked:
PastorDwayne
  • 8
  • 6
1 Solution
 
madfaceCommented:
use the submit function inside IHTMLFormElement
that will work
0
 
madfaceCommented:

you do it the same as  you would with a client side script  ie  javascript
example code
assume p_HtmlDoc is of type IHTMLDocument2

IHTMLElementCollection* p_eles = NULL;
IHTMLFormsElement* p_formEle = NULL;
pHtmlDoc->get_forms(&p_eles);

//iteratatet through  the collection untill you find the
//form.  On yahoo i think  there  is only  1 form so it
//be at position 0
//once its found do the following


p_formEle.submit();

//done


0
 
PastorDwayneAuthor Commented:
I seem to be getting an error when I try to complie this

"error C2228: left of '.submit' must have class/struct/union type"

Here is the code I'm using:
{
MSHTML::IHTMLDocument2Ptr pDoc= m_ctrBrowser.GetDocument();
MSHTML::IHTMLElementCollectionPtr pAllElems= pDoc->all;

MSHTML::IHTMLElementCollection* p_eles = NULL;
MSHTML::IHTMLFormElement* p_formEle = NULL;
pDoc->get_forms(&p_eles);

p_formEle.submit();

}
were m_ctrBrowser is the ActiveX web control


Do you see any problems that would cause this error?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PastorDwayneAuthor Commented:
I have now changed p_formEle.submit(); to p_formEle->submit(); thinking that this was the problem.

It is able to compile now, but it doesn't submit the form.
Any suggestions?
0
 
madfaceCommented:

Sorry i should have been more clear.
the problem is that  p_formEle  is still NULL
do this

//we have to make a new collections for the forms
IHTMLElementCollection formEles = NULL;

_variant_t tagName("FORM");
p_ele->tags(tagName, &formEles);



//since we know there is only 1 form on the page we know
//its in the 0 index position

_variant_t index(0);
formEles->item(index, index, &p_formEle);
//now p_formEle is set and is able to use

//check to see if we do have the form
if(!p_formEle)
    {
    //error code here
    }
p_formEle.submit();

i havent tried any of this code in my compiler so there my  be compiler errors.
0
 
madfaceCommented:
oops last line should be
sorry thats twice now
p_formEle->submit();
0
 
PastorDwayneAuthor Commented:
I have tried to work with this code, but have had no luck.  After I fixed the compile errors, It would crash the program when it was executed.

I wonder if there are any more suggestions on how to do this?

thanks very much.
0
 
madfaceCommented:
ok whole code  all over again i just did this in MFC and it worked flawlessly



pHtmlDoc = myWebBrowser.GetDocument();

IHTMLElementCollection* p_eles = NULL;
IHTMLFormsElement* p_formEle = NULL;
pHtmlDoc->get_forms(&p_eles);

_variant_t index((LONG)0);
p_eles->item(index, index, &p_formEle);

p_formEle->submit();


good luck
0
 
madfaceCommented:
sorry if the first two didnt work
i assumed you used HTML Dom and knew how to get elements in the Dom
0
 
PastorDwayneAuthor Commented:
madface,

Everthing seems to compile O.K except for the line:

p_eles->item(index, index, &p_formEle);

the compiler returns to state that ->item does not take three paramiters.

any suggestions?

0
 
madfaceCommented:
im using raw COM functions
so the last param is really a return value
so try this
p_formEle = p_eles->item(index, index);

are you using a  sdk  or  the #import "\path\mshtml.tlb"

if your using the  import  try  this

p_formEle->AttachDispatch(p_eles->item(index, index));

man com sucks

now one of those 2 ways should  work (knocks on wood)
0
 
PastorDwayneAuthor Commented:
madface,

I still seem to be having troubles with the one line.  Here is my situation:

I do have #import "c:\winnt\system32\mshtml.tlb" in StdAfx.h.  I have tried:
p_formEle->AttachDispatch(p_eles->item(index, index));

but it states that :"AttachDispatch' : is not a member of 'IHTMLFormElement'"

I also tried using:
p_formEle = p_eles->item(index, index);
But is states when being compiled:
"error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class  (or there is no acceptable conversion)"


Here is my code:


{
MSHTML::IHTMLDocument2Ptr pDoc= m_ctlBrowse.GetDocument();
MSHTML::IHTMLElementCollection* p_eles = NULL;
MSHTML::IHTMLFormElement* p_formEle = NULL;
pDoc->get_forms(&p_eles);
_variant_t index((LONG)0);

p_formEle->AttachDispatch(p_eles->item(index, index));
// p_formEle = p_eles->item(index, index); //or this line

p_formEle->submit();    
}


Do you see anything that is out of the ordinary or is a problem?

Thanks very much...
0
 
madfaceCommented:

change  MSHTML::IHTMLFormElement  to
MSHTML::IHTMLFormElementPtr

and use  
 p_formEle = p_eles->item(index, index);

lets cross  our fingers



0
 
PastorDwayneAuthor Commented:
madface,

I tryed this out and it works perfectly! who would have guessed...

thanks for your help...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now