简体   繁体   中英

WebBrowser in C# console application won't navigate

I can't get the WebBrowser class to open a website.

Here's my code:

    using System;
    using System.Windows.Forms;

    // This is pseudo code
    class Program
    {
        private static String mail = "example@mail.com";
        private static String password = "randomPassword";

        private static String url = "http://www.loginUrl.com";

        [STAThread]
        static void Main(string[] args)
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.Navigate(url);

            Console.ReadKey();
        }

        void AutoLogin()
        {
            SendKeys.SendWait(mail);
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait(password);
            SendKeys.SendWait("{ENTER}");
        }
    }

I read through multiple forums, where users had the same problem but I can't seem to fix it. It just doesn't make any sense. Please don't try to give me other methods than using webbrowser. I want to be able to know when the desired page finished loading that's why I'm using webbrowser and not Process.Start() or something. Thanks for your help.

You can open browser with the following command:

System.Diagnostics.Process.Start(url);

In the sample you provided you create WebBrowser control, navigate in it and dispose it. Maybe you need WinForms App instead of Console app and put your WebBrowser on the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM