?
Solved

Looking to enumerate HTML elements on a .NET Web page

Posted on 2008-10-17
8
Medium Priority
?
581 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 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