在浏览器中打开一个URL并将值设置到特定元素中。

huangapple go评论70阅读模式
英文:

Open an URL in Browser and set value to specific element

问题

我曾经有一个应用程序,我在应用程序中打开一个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?

英文:

I had an application where I used to open an URL from my application in browser and set some values in different elements of that page. Code like below:

            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;

It is working fine. But recently some of my clients have advanced sites where InternetExplorer is not working. Instead of InternetExplorer I want to use Microsoft Edge.
I tried as below (after installing WebView2 from nuget) :

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

But still no way to replace InternetExplorer. Is there any proper way to use Edge or Chrome?

答案1

得分: 1

你的要求“在浏览器中打开URL并设置元素值”听起来更像是浏览器自动化。您可以检查是否可以使用WebDriver自动化Edge浏览器来满足您的需求。这个代码示例展示了如何导航到URL并设置输入值。

英文:

Your requirement "open a URL in browser and set values in elements" heard more like browser automation. You can check if using WebDriver to automate Edge can meet your requirement. This code sample shows how it navigates to url and sets an input value.

huangapple
  • 本文由 发表于 2023年3月1日 13:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定