iCantSeeSharp
iCantSeeSharp

Reputation: 3880

catch user activity within webbrowser or watin

Is there any way to detect all the possible user activities within a WebBrowser and return a custom Event? For example: User clicks on "search" button, return "SearchButtonClicked" custom Event.

It would be something like, logging of all the activity that user does, stored in a sequence and could be automated once he wanted.

Edit: I do not own the webpage. I am trying to make an application to automate some searching on google.

Upvotes: 2

Views: 1302

Answers (3)

FarligOpptreden
FarligOpptreden

Reputation: 5043

Wow, it's going to be quite a bandwidth intensive application... :) You might consider a framework like jQuery to attach events to all anchors and button-type inputs to perform AJAX calls to your server, for example. So you might have something along the lines of the following process:

Include a JS file on all your pages to do something like the following:

$(document).ready(function () {
    var trackUserActivity = function(elementId, elementText) {
        $.ajax({
            type: "POST",
            url: "url/TrackUserActivity",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                ElementId: elementId,
                ElementText: elementText
            }),
            success: function (result) {
                // do something if call was successful
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // do something if an error occurred
            }
        });
    };
    $("a, input[type=\"button\"], input[type=\"submit\"]").click(function() {
        trackUserActivity($(this).attr("id"), $(this).text());
    });
});

Create a Web Method that can be called via AJAX to track the user activity:

[WebMethod]
public static void TrackUserActivity(string ElementId, string ElementText)
{
    // Implement your user activity tracking logic, like saving it in a database
}

After your OP edit, you don't own the application, so this won't work. I'm keeping the answer here for someone in the future who might have a similar need.

Upvotes: 3

Harshal Doshi  Jain
Harshal Doshi Jain

Reputation: 2597

See types of HtmlElementEventHandler: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement_events%28v=vs.110%29.aspx

Use this event handler for knowing event occured. On event occured, get the element details, tag details and log in the file with the type of event.

See usage of downcasting required when using HtmlElementEventHandler http://www.hanselman.com/blog/BackToBasicsThisIsNotTheObjectYoureLookingwaitOhItIsTheObject.aspx.

When you want to replay action, you may run logged events in sequence, after extracting tag name and value fields.

Upvotes: 0

iCantSeeSharp
iCantSeeSharp

Reputation: 3880

After some research, I discovered the HtmlElementEventHandler.

Example:

webBrowser1.Document.GetElementById("MainContent_LoginButton").Click += new HtmlElementEventHandler(test);

// some code...

public void test(object sender, HtmlElementEventArgs e)
{
    MessageBox.Show("Clicked login button");
}

Upvotes: 3

Related Questions