Solved

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

Posted on 2011-02-28
5
2,156 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now