Solved

Looking to enumerate HTML elements on a .NET Web page

Posted on 2008-10-17
8
580 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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