?
Solved

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

Posted on 2011-02-28
5
Medium Priority
?
2,260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Several part series to implement Internet Explorer 11 Enterprise Mode
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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…
Suggested Courses

752 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