C # allows WebBrowser to run JavaScript scripts for various automated actions, such as clicking on a button on a webpage, entering a username password, and so on. The code is also simple:
>>>>>>>>>>>>>>>>>>>> Method One:
You only need to get the htmlelement first and then manipulate the acquired element, for example:
- htmlelement btn = webBrowser1. Document. getElementById("abtn");
- if (btn! = null)
- BTN. InvokeMember("click");
>>>>>>>>>>>>>>>>>>>> Method Two:
To inject JavaScript code into a page:
- HtmlElement ele = webBrowser1. Document. CreateElement("script");
- Ele. SetAttribute("type", "Text/javascript");
- Ele. SetAttribute("text", "Somejavascript");
- WebBrowser1. Document. Body. AppendChild(ele);
JavaScript is executed automatically after the last sentence is executed
>>>>>>>>>>>>>>>>>>>> Method Three:
Run the script already in the page:
- WebBrowser1. Document. Invokescript("Someinpagejavafunction", null);
>>>>>>>>>>>>>>>>>>>> In addition:
In addition to loading the Web page in WebBrowser, if you encounter a script error, the default pop-up dialog box, which is sometimes annoying, but can be closed:
- WebBrowser. scripterrorssuppressed = true;
Let WebBrowser run JavaScript script in C #