C# WebBrowser Invoke issue

Posted by James Jeffrey on Stack Overflow See other posts from Stack Overflow or by James Jeffrey
Published on 2010-03-27T12:27:42Z Indexed on 2010/03/27 12:33 UTC
Read the original article Hit count: 287

Filed under:

I am logging into facebook using a web browser. Everything works, but the problem is when I invoke the button click I need to check if the password is correct but, the check seems to happen before the button is invoked which makes no sense at all because the checking code is after the invoke.

private void Facebook_Login(String username, String password)
    {
        webBrowser1.Url = new Uri("http://m.facebook.com");

        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();

        HtmlElementCollection inputs = webBrowser1.Document.GetElementsByTagName("input");
        foreach(HtmlElement input in inputs)
        {
            if (input.GetAttribute("name") == "email") 
            {
                input.SetAttribute("value", "[email protected]");
            }
            if (input.GetAttribute("name") == "pass")
            {
                input.SetAttribute("value", "kelaroostj"); // dont worry that pass wont work lol.
            }
            if (input.GetAttribute("name") == "login")
            {
                input.InvokeMember("click");
            }
        }

        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();

        HtmlElementCollection bs = webBrowser1.Document.GetElementsByTagName("b");
        foreach(HtmlElement b in bs)
        {
            MessageBox.Show(b.InnerHtml);
        }

        Log_Message("Logged into Facebook with: [email protected]");
    }

© Stack Overflow or respective owner

Related posts about c#