欢迎访问!
您的支持是我们不断前行的动力!

C# 动态声明webBrowser自动登录网站

该方法已被淘汰,在有其他方法的情况下一般不使用该方法

using System;
using System.Threading;
using System.Windows.Forms;

namespace WebBrowserThreadModeExample
{
    public partial class MainForm : Form
    {
        private Thread webThread;
        private WebBrowser webBrowser;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            webThread = new Thread(InitializeWebBrowser);
            webThread.SetApartmentState(ApartmentState.STA); 
            webThread.Start();
        }

        private void InitializeWebBrowser()
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.ScriptErrorsSuppressed = true;
            webBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
            webBrowser.Navigate("https://www.example.com/login");
            Application.Run();
        }

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //方法一
            if (webBrowser.Url.AbsoluteUri == "https://www.example.com/login")
            {
                HtmlElementCollection inputElements = webBrowser.Document.GetElementsByTagName("input");
                foreach (HtmlElement element in inputElements)
                {
                    if (element.GetAttribute("className") == "username-input")
                    {
                        element.SetAttribute("value", "your_username");
                    }
                    else if (element.GetAttribute("className") == "password-input")
                    {
                        element.SetAttribute("value", "your_password");
                    }
                }
                HtmlElementCollection buttonElements = webBrowser.Document.GetElementsByTagName("button");
                foreach (HtmlElement element in buttonElements)
                {
                    if (element.GetAttribute("className") == "login-button")
                    {
                        element.InvokeMember("click");
                        break;
                    }
                }
            }
            //方法二
            if (webBrowser.Url.AbsoluteUri == "https://www.example.com/login")
            {
                HtmlElement usernameElement = webBrowser.Document.GetElementById("username");
                HtmlElement passwordElement = webBrowser.Document.GetElementById("password");
                if (usernameElement != null && passwordElement != null)
                {
                    usernameElement.SetAttribute("value", "your_username");
                    passwordElement.SetAttribute("value", "your_password");
                    HtmlElement loginButton = webBrowser.Document.GetElementById("login-button");
                    if (loginButton != null)
                    {
                        loginButton.InvokeMember("click");
                    }
                }
            }
            //任务结束后再结束线程
            webThread.Abort();
        }
    }
}
赞(0) 打赏
转载请注明出处,未注明出处不得转载,蜘蛛侠 » C# 动态声明webBrowser自动登录网站
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

蜘蛛侠,您身边的爬虫专家!

觉得文章有用就打赏一下文章作者吧

支付宝扫一扫打赏

微信扫一扫打赏