Search code examples
c#internet-explorermicrosoft-edge

Open an URL in Browser and set value to specific element


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?


Solution

  • 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.