views:

51

answers:

4

I have a web page loaded into a WebBrowser object. What I want to do is access the elements on that page to input data. For example, enter username and password and submit the form.

How is this possible? Any ideas?

Could I use HTMLAgilityPack to access the elements and set their values?

A: 

There are lots of solutions. Two that I've used myself are:

WaitN - http://watin.sourceforge.net/ WebAii - apparently purchased by Telerik? There used to be a free version, not sure if there is any longer - http://www.artoftest.com/index.html

Then there's just using automation against the WebBrowser control. An automation framework makes it so much simpler.

Paul Kearney - pk
I vaguely remember WaitiN. I'm pretty sure that's what I used last year when creating a web bot. I'll check it out. Thanks for refreshing my memory.
James Jeffery
Yes it seems to be available on SourceForge. When I just searched my computer I found the .dll. Not been on this PC for a while.Thanks again Paul.
James Jeffery
+1  A: 

Hello,

Check out this documentation example: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.aspx

Through the web browser control, there is a Document property of type HtmlDocument, which gives you some ability to affect the page. I personally don't know if it can do everything you want, but this would be a good starting point.

HTH.

Brian
A: 

Also check out this example which is using HtmlAgilityPack to do a POST request with username and password fields:

http://refactoringaspnet.blogspot.com/2010/04/using-htmlagilitypack-to-get-and-post.html

Rohit Agarwal
A: 

I use a combination of HtmlAgilityPack and fiddler to watch the traffic. With fiddler you can see the post that is being made to the server, and just replicate it. Then you can capture the cookie data to save the login session. From there its just normal navigating around the site.

A few things to watch out for, if you use a login to get the data there may be a cookie. If so you need to set it in the cookieContainer. This is a sample function that I use to post data to an url.

    private string PostWebRequestContent(Uri uri, string postData)
    {
        CookieContainer cookies = LoggedInCookie();
        foreach (Cookie c in cookies.GetCookies(uri))
        {
            int i = 0;
        }

        HttpWebRequest req = GetWebRequest(uri);
        req.CookieContainer = cookies;


        req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, application/vnd.xfdl; version=7.5.0.51, application/x-shockwave-flash, */*";


        req.Method = "POST";
        req.Headers.Add("Accept-Language: en-us");
        //req.Headers.Add("Accept-Encoding: gzip,deflate");
        //req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        req.KeepAlive = true;
        req.ContentType = "application/x-www-form-urlencoded";

        StreamWriter requestWriter = new StreamWriter(req.GetRequestStream());
        requestWriter.Write(postData);
        requestWriter.Close();


        string sid = "";

        try
        {
            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                {
                    sid = sr.ReadToEnd().Trim();
                }
            }
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Message);
        }

        return sid;
    }

Cookie session function

    private CookieContainer GetSessionCookie(Uri uri)
    {
        HttpWebRequest req = GetWebRequest(uri);

        CookieContainer cookies = new CookieContainer();
        req.CookieContainer = cookies;

        req.GetResponse().Close();

        return cookies;
    }
Matt Heffernan