我有一个应用程序,我过去常常在浏览器中从我的应用程序打开一个 URL,并在该页面的不同元素中设置一些值。代码如下:
Object o = null;
Object URL = url;
mshtml.HTMLDocument doc;
InternetExplorer ie = new InternetExplorer();
ie.Visible = true;
ie.Navigate2(ref URL, ref o, ref o, ref o, ref o);
while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
doc = (mshtml.HTMLDocument)ie.Document;
mshtml.HTMLInputElement el = (mshtml.HTMLInputElement)doc.all.item("credentials-email", null);
el.value =un;
el = (mshtml.HTMLInputElement)doc.all.item("credentials-password", null);
el.value = ps;
工作正常。但最近我的一些客户拥有高级站点,其中 InternetExplorer 无法正常工作。我想使用 Microsoft Edge 而不是 InternetExplorer。 我尝试如下(从 nuget 安装 WebView2 之后):
Object o = null;
Object URL = url;
mshtml.HTMLDocument doc;
Microsoft.Web.WebView2.WinForms.WebView2 webView = new Microsoft.Web.WebView2.WinForms.WebView2();
webView.CreationProperties = new Microsoft.Web.WebView2.WinForms.CoreWebView2CreationProperties();
webView.CreationProperties.UserDataFolder = userDataFolder;
webView.CreationProperties.Args = new string[] { "--disable-web-security", "--disable-features=CrossSiteDocumentBlockingIfIsolating" };
webView.CreationProperties.StartInWebView = true;
webView.CreationProperties.BrowserExecutableFolder = @"C:\Program Files (x86)\Microsoft\Edge\Application";
webView.CreationProperties.BrowserExecutableArguments = "--disable-gpu";
webView.Dock = DockStyle.Fill;
this.Controls.Add(webView);
webView.CoreWebView2InitializationCompleted += async (e, url) =>
{
await webView.CoreWebView2.AddWebResourceRequestedFilter("*", Microsoft.Edge.WebView2.Core.CoreWebView2WebResourceContext.All);
await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(@"document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded'); });");
await webView.CoreWebView2.AddScriptToExecuteOnDocumentCompletedAsync(@"console.log('DocumentCompleted');");
webView.CoreWebView2.Navigate(url);
};
但是还是没办法替代InternetExplorer。 Edge 或 Chrome 有什么正确的使用方法吗?