Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-28
5
Medium Priority
?
2,302 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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