Let WebBrowser run JavaScript script in C #

Source: Internet
Author: User

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:

  1. htmlelement btn = webBrowser1. Document. getElementById("abtn");
  2. if (btn! = null)
  3. BTN. InvokeMember("click");


>>>>>>>>>>>>>>>>>>>> Method Two:
To inject JavaScript code into a page:

  1. HtmlElement ele = webBrowser1. Document. CreateElement("script");
  2. Ele. SetAttribute("type", "Text/javascript");
  3. Ele. SetAttribute("text", "Somejavascript");
  4. WebBrowser1. Document. Body. AppendChild(ele);

JavaScript is executed automatically after the last sentence is executed


>>>>>>>>>>>>>>>>>>>> Method Three:
Run the script already in the page:

    1. 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:

      1. WebBrowser. scripterrorssuppressed = true;

Let WebBrowser run JavaScript script in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.