Solved

Web Browser Control - How to get IHTMLDocument2 for pages with frames

Posted on 2011-02-28
5
2,125 Views
Last Modified: 2012-05-11
I am working with a power switch which has a browser based user interface, very similar to any router interface. To automate it through my application I have inserted microsoft browser control. I plan to open the setup page in browser control and navigate to the setting to change it.

I have been able to log in automatically with user name and password. But once I log in, it uses frames to display the settings, like any typical router setup page. I have no idea how to get the IHTMLDocument2 for the right hand frame HTML from here.

Any help is highly appreciated. I have pasted the code below I am using for logging into the switch.
void* CPDlg::ElemFromID( LPWSTR szID, IID nTypeIID )
{
	HRESULT hr;
	IHTMLDocument2* pDoc= (IHTMLDocument2*) m_webCtrl.get_Document();
	IHTMLElementCollection* pAll=  NULL;
 
	hr= pDoc->get_all( &pAll ); 

	CComVariant vElement( szID ); // the id or name of the control
	CComVariant vIndex(0,VT_I4);  // 0 (presume it's not a collection)
 
	IDispatch* pDisp;
 	hr= pAll->item(vElement,vIndex,&pDisp);
 
	void* pElement;   // will coerce to desired type later
 	hr= pDisp->QueryInterface( nTypeIID,(void**)&pElement);

	return( pElement );
}

bool CPDlg::LogIn()
{
	void* pElem= ElemFromID(L"Username", IID_IHTMLInputElement) ;
	IHTMLInputElement* pTextBoxUser= (IHTMLInputElement*)pElem;

	pTextBoxUser->put_value( L"admin" ); // populate a text box!

	pElem= ElemFromID(L"Password", IID_IHTMLInputElement) ;
	IHTMLInputElement* pTextBoxPswd= (IHTMLInputElement*)pElem;

	HRESULT hr= pTextBoxPswd->put_value( L"12345678" ); 

	pElem= ElemFromID(L"Submitbtn", IID_IHTMLElement);
	IHTMLElement* pSubmit= (IHTMLElement*)pElem;

	hr= pSubmit->click(); 
	bLoggedIn = true;

	return true;

}

Open in new window

0
Comment
Question by:vijrohit7
  • 2
5 Comments
 

Author Comment

by:vijrohit7
ID: 35003118
Adding html files from the interface I am trying to control.   HTML-with-Frames.zip
0
 
LVL 8

Accepted Solution

by:
SunnyDark earned 500 total points
ID: 35008195
Hi,
get_Frames() to get IHTMLFramesCollection2
Then iterate the collection to find your frame.
Use QueryInterface to get IHTMLWindow2 from it.
Then use get_document in the resulting IHTMLWindow2  to get the document.

That's it...
0
 

Author Comment

by:vijrohit7
ID: 35009886
Thanks SunnyDark, I got it working...the working code is attached below.
Now I am facing this problem...there are two radio buttons on the page with same name("P60"). In the code  below it's clicking on the first one but I want to click on the second one. How do I do that?

HRESULT hr;
	IHTMLDocument2* pDoc= (IHTMLDocument2*) m_webCtrl.get_Document();

	IHTMLFramesCollection2 *pAllFrames = NULL;
	hr = pDoc->get_frames(&pAllFrames);

	CComVariant vIndex(1,VT_I4);  // Get frame at location 1
	CComVariant vFrame(VT_DISPATCH);  

	hr = pAllFrames->item(&vIndex, &vFrame);

	void* pVoid;
	hr= vFrame.pdispVal->QueryInterface( IID_IHTMLWindow2,(void**)&pVoid);
	IHTMLWindow2* pWin= (IHTMLWindow2*)pVoid;
	IHTMLDocument2* pDocF0;
	pWin->get_document(&pDocF0);

	IHTMLElementCollection* pAll=  NULL;
	hr= pDocF0->get_all( &pAll ); 

	//click ON
	CComVariant vElementF0( L"P60" ); // the id or name of the control
	CComVariant vIndexF0(0,VT_I4);  // 0 
	IDispatch* pDisp;
 	hr= pAll->item(vElementF0, vIndexF0, &pDisp);
	void* pElement;   
 	hr= pDisp->QueryInterface( IID_IHTMLElement,(void**)&pElement);
	IHTMLElement* pSubmit= (IHTMLElement*)pElement;
	hr= pSubmit->click();

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35321692
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now