How to inject Javascript in WebBrowser control?

For some reason Richard’s solution didn’t work on my end (insertAdjacentText failed with an exception). This however seems to work: HtmlElement head = webBrowser1.Document.GetElementsByTagName(“head”)[0]; HtmlElement scriptEl = webBrowser1.Document.CreateElement(“script”); IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement; element.text = “function sayHello() { alert(‘hello’) }”; head.AppendChild(scriptEl); webBrowser1.Document.InvokeScript(“sayHello”); This answer explains how to get the IHTMLScriptElement interface into your project.

How can I get the WebBrowser control to show modern contents?

Note: The post is about WebBrowser control, however, for all the new .NET projects the main solution is using WebView2. To learn more, take a look at this post: Getting started with WebView2. WebBrowser Control The WebBrowser control uses the same Internet Explorer version which is installed on your OS but it doesn’t use the … Read more

C# WebBrowser Ajax call

WebBrowser control (both WPF and WinForms versions) behaves in many ways differently from the full IE. You may want to implement Feature Control to bring its behavior as close to IE as possible (particularly, FEATURE_BROWSER_EMULATION), this often solves the script compatibility issues. Here is some code, note that it does not require admin rights to … Read more