I'm trying to programmatically login to a site like espn.com. The way the site is setup is once I click on the Log In button located on the homepage, a Log In popup window is displayed in the middle of the screen with the background slightly tinted. My goal is to programmatically obtain that popup box, supply the username and password, and submit it -- hoping that a cookie is returned to me to use as authentication. However, because Javascript is used to display the form, I don't necessarily have easy access to the form's input tags via the main page's HTML.

I've tried researching various solutions such as HttpClient and HttpWebRequest, however it appears that a Webbrowser is best since the login form is displayed using Javascript. Since I don't necessarily have easy access to the form's input tags, a Webbrowser seems the best alternative to capturing the popup's input elements.

class ESPNLoginViewModel
    private string Url;
    private WebBrowser webBrowser1 = new WebBrowser();
    private SHDocVw.WebBrowser_V1 Web_V1;

    public ESPNLoginViewModel()

    private void Initialize()
        Url = "http://www.espn.com/";

    private void Login()
        webBrowser1.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);

        Web_V1 = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;
        Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_V1_NewWindow);

    //This never gets executed
    private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        //I'll start determing how to code this once I'm able to get this invoked

    private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        HtmlElement loginButton = webBrowser1.Document.GetElementsByTagName("button")[5];


        //I've also tried the below InvokeScript method to see if executing the javascript that
        //is called when the Log In button is clicked, however Web_V1_NewWindow still wasn't called.

I'm expecting the Web_V1_NewWindow handler to be invoked when the InvokeMember("click") method is called. However, code execution only runs through the webpage_DocumentCompleted handler without any calls to Web_V1_NewWindow. It might be that I need to use a different method than InvokeMember("click") to invoke the Log In button's click event handler. Or I might need to try something completely different altogether. I'm not 100% sure the Web_V1.NewWindow is the correct approach for my needs, but I've seen NewWindow used often when dealing with popups so I figured I should give it a try.

Any help would be greatly appreciated as I've spent a significant amount of time on this.

0 Answers