1

I need to design a multi-threaded application to login to a website. So far I am able to load to the page without using threading. But I am stuck in login button click el.InvokeMember("Click");, it is not getting past this and loading the main page. In my example i am using Pintrest login, any help to design a multi-threaded console app to login to a web page will be great.

Below is my code:

 private static bool completed = false;
    static string body = "";
    private static WebBrowser wb;
    [STAThread]
    static void Main(string[] args)
    {
        //http://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread
        //http://www.codeproject.com/Questions/197007/How-Use-WebBrowser-without-winform
        wb = new WebBrowser();
        wb.ScriptErrorsSuppressed = true;
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LoginPageLoadedEvent);
      //  wb.DocumentCompleted += LoginPageLoadedEvent;

        wb.Navigate("https://www.pinterest.com/login");
        while (!completed)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }

        Console.Write("\n\nDone with it!\n\n");


    }

    static void LoginPageLoadedEvent(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        wb.DocumentCompleted -= LoginPageLoadedEvent;

        HtmlElementCollection theElementCollection = wb.Document.GetElementsByTagName("Input");
        foreach (HtmlElement curElement in theElementCollection)
        {
            string controlName = curElement.GetAttribute("name").ToString();
            if (controlName == "username_or_email")
            {
                curElement.SetAttribute("Value", "test@gmail.com");
            }
            if (controlName == "password")
            {
                curElement.SetAttribute("Value", "test123");
            }
        }


        HtmlElementCollection elc = wb.Document.GetElementsByTagName("button");
        foreach (HtmlElement el in elc)
        {
            if (el.GetAttribute("type").Equals("submit"))
            {
                el.InvokeMember("Click");
                break;
            }
        }

        wb.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(LoginCompleteMainPageLoadedEvent);
        while (wb.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }



    }

    static void LoginCompleteMainPageLoadedEvent(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        //Reading html data into text file
        string s = ((WebBrowser)sender).DocumentText;


        Console.WriteLine(wb.Document.Body.InnerHtml);
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\temp\\test.txt");
        file.WriteLine(wb.Url.ToString());
        file.WriteLine(s);
        file.Close();

        wb.DocumentCompleted -= LoginCompleteMainPageLoadedEvent;
    }

    static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (wb.ReadyState != WebBrowserReadyState.Complete)
            return;

        if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
            return;

        if (body == wb.Document.Body.InnerHtml) 
            return;

        body = wb.Document.Body.InnerHtml;


        Console.WriteLine(wb.Document.Body.InnerHtml);
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\temp\\test1.txt");
        file.WriteLine(wb.Url.ToString());
        file.WriteLine(wb.DocumentText);
        file.Close();
        completed = true;
    }
}
Prateek Regmi
  • 53
  • 1
  • 6

1 Answers1

0

Try this:

webBrowser1.Document.GetElementsByTagName("Form")[2].InvokeMember("submit");
mmushtaq
  • 3,430
  • 7
  • 30
  • 47
Larry
  • 1