使用多线程C#console应用程序登录网站

时间:2015-08-19 04:20:25

标签: c# multithreading web-scraping

我需要设计一个多线程应用程序来登录网站。到目前为止,我可以加载到页面而不使用线程。但我被困在登录按钮点击el.InvokeMember("点击");,它没有超过这个并加载主页面。在我的示例中,我正在使用Pintrest登录,任何设计多线程控制台应用程序以登录网页的帮助都会很棒。

以下是我的代码:

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

1 个答案:

答案 0 :(得分:0)

试试这个:

webBrowser1.Document.GetElementsByTagName("Form")[2].InvokeMember("submit");