一、新建Web项目
@{ViewData["Title"] = "Home Page";Layout = null;
}
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
<head><script type="text/javascript">$(function () {$('#btnLogin').click(function () {alert('登录成功!');})})</script>
</head>
<div class="text-center"><form>用户名:<input type="text" id="txtUserName" /><br /><br />密 码:<input type="password" id="txtPwd" /><br /><br /><input type="button" id="btnLogin" value="登录" onclick="login" /></form>
</div>
二、新建Winform程序
安装:CefSharp.WinForms
代码:
using CefSharp.WinForms;
using CefSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;namespace CefDemo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){var url = $"http://localhost:5220/";chromiumWebBrowser1.LoadUrl(url);chromiumWebBrowser1.LoadingStateChanged += ChromiumWebBrowser1_LoadingStateChanged;}private void ChromiumWebBrowser1_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e){if (e.IsLoading == false){var userName = "guangtouqiang";var str = "";for (int i = 0; i < userName.Length; i++){chromiumWebBrowser1.ExecuteScriptAsync("document.getElementById('txtUserName').focus();document.getElementById('txtUserName').value='" + str + "';");Thread.Sleep(500);str += userName[i];}var password = "123456";str = "";for (int i = 0; i < password.Length; i++){chromiumWebBrowser1.ExecuteScriptAsync("document.getElementById('txtPwd').focus();document.getElementById('txtPwd').value='" + str + "';");Thread.Sleep(500);str += password[i];}chromiumWebBrowser1.ExecuteScriptAsync("document.getElementById('btnLogin').focus();document.getElementById('btnLogin').click();");}}}
}
运行效果: