catch user activity within webbrowser or watin

笑着哭i 提交于 2019-12-11 02:51:18

问题


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.


回答1:


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:

1) 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());
    });
});

2) 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
}

BIG IMPORTANT EDIT: Well, seeing as, after your OP edit, you don't own the application, this won't work. I'm keeping the answer here for someone in the future who might have a similar need, so please don't down-vote based on it not addressing the OP specifically.




回答2:


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");
}



回答3:


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.



来源:https://stackoverflow.com/questions/8908710/catch-user-activity-within-webbrowser-or-watin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!