Solved

Looking to enumerate HTML elements on a .NET Web page

Posted on 2008-10-17
8
576 Views
Last Modified: 2013-11-20
I'm running a .NET Web application that I would like to automate via Visual C++/MFC/COM. How can I enumerate the fields/controls on the page in terms of getting their ids and names.
0
Comment
Question by:tma050898
8 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22748281
If you have a CHtmlView derived class for showing the web page, you should be able to enumerate  all controls by GetWindow calls

void MyHtmlView::OnInitialUpdate(...)
{
     CWnd* pWnd = GetWindow(GW_CHILD);

     while (pWnd != NULL)
     {
            // here the pWnd is one of the controls you were looking for
            ...
            // next control
            pWnd = GetWindow(GW_HWNDNEXT);
     }
}

}
0
 
LVL 3

Author Comment

by:tma050898
ID: 22748650
Sorry guys. I must have worded this very poorly. I'll try to do better this time.

My company has an ASP.NET application to which I have no access to the source code. I'm simply an end-user. Being 99% Windows/MFC developer, I know how to manipulate Windows apps very well. However, what I want to do is to automate this blasted Web "app".

As an example, the page has several edit controls and listboxes in which I need to enter data and make selections before hitting the Submit button. This is what I want to automate. However, I can't figure out the IDs or Names of the controls on the page (View Source didn't help). I'm using the MSHTML library, but am not getting back the element pointer so instead of me posting my code, I figured I would ask a generic question of "How can I enumerate all of the controls' IDs/Names" on the page from an MFC/COM application?

Hopefully, that makes it clearer. Please ask any questions - I'll share the small bit of source code I have if that'll help but that code actually attempts to get one specific field.

Thanks!
Tom
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22749336
>>>>> "How can I enumerate all of the controls' IDs/Names"
You want get them by not programming?

A control is a window where you need the hwnd handle to retrieve the ID of which is a numeric (binary) number. You would need the control style and class id as well in order to find out whether it is a combobox, an edit or a button. The only non-programmatical approach I could think of is to using Spy of the Visual Studio tools (but I wonder what you want to do with that information if you have no program?). Note automation is also another thing than to enumerate controls. First the web application you want to control must have an automation interface app where you have to retrieve an interface of. Then, after getting a pointer of that interface you could call 'methods' this interface provides.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 3

Author Comment

by:tma050898
ID: 22749361
[itsmeandnobodyelse] You want get them by not programming?
[Tom] As I mentioned, I'm attempting to automate this Web app from an MFC/COM application.

My code is loading the active IE document, but I can't figure out how to access the specific fields within the ASP.NET application as I can't see the code-behind and therefore, don't know the field names. Therefore, I'd like to enumerate all of the application's fields/controls.



0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22749694
>>>> My code is loading the active IE document,
The ASP.NET application is running on some web server. It's code is only available to you if the author of the the website has made an automation interface. What most likely comes to your local computer is simple html. No controls, no ids. All other things would require a local component installed. In case the web application is a .NET assembly, your local .NET environment could provide some local functions but I really doubt that the page you see on screen is more than one single child window of the IE what actually was "painted" and where there is nothing such as controls which you could enumerate.  
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 22750586
It sounds like you want to access the HTML page as a document ("DOM manipulation"), which contains collections such as INPUT controls, Images, links, etc.  That is certainly possible using a number of techniques.  The easiest might be to create an HTA that loads the page and uses simple JavaScript syntax like:
    var oTextArea= gwnd.document.all.SomeTextFieldId;
    oTextArea.value= "Hi there!";

Since you are using MFC, I suggest making a simple dialog-based app and placing a webbrowser control on it.  Then you load the page and use C++ statements to access the ovjects available in the IHTMLDocument2 interface.  There are many examples of that here on EE and elsewhere.  Here's a pretty good one:

    Web based program
    http://www.experts-exchange.com/Programming/Languages/CPP/Q_20272700.html
but you can search  EE (or elsewhere) for the word  
   IHTMLDocument2Ptr
to get a lot of info.
One important "gotcha" with this technique is that you MUST wait until the OnDocumentComplete function is called before trying read or modify ovjects in the DOM.
0
 
LVL 3

Accepted Solution

by:
Naveen_R earned 125 total points
ID: 22754665
You can get all controls in the web page using the IWebBrowser2 interfaces..Check the below sample...

http://www.codeproject.com/KB/COM/DHTML_Interfaces.aspx
0
 
LVL 3

Author Comment

by:tma050898
ID: 22781377
FYI: Thanks to everyone that posted. I'm heads-down on some other tasks right now, but will read through and test these different solutions this weekend.

Thanks again!
Tom
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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