Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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