Bill: WebBrowser Execute and install jquery script (Iebrowse)

Source: Internet
Author: User

The previous article showed how to get the JS script to access the classes in. NET, this time to show you how to install JQuery scripts on any page and manipulate the elements on the page.

In fact, in the first article on Iebrowser , there has been an example of jQuery, but probably because of the length of reason, there is not too much explanation, so decided to write a separate article to re-explain.

This Preparation example is also relatively simple, to achieve the simulation of user search Baidu, please look at the following code:

if (string. IsNullOrEmpty (This.txtKeyword.Text))
Return

Iebrowser ie = new Iebrowser (this.webbrowser);

Ie. Navigate ("http://www.baidu.com/");

Ie. Ieflow.wait (New Urlcondition ("Wait", "http://www.baidu.com/", Stringcomparemode.startwith));

Ie. Installjquery (jquery.codemin);

Ie. Executejquery (Jquery.create ("#kw"). Val ("'" + this.txtKeyword.Text.Trim () + "'"));

Ie. Executejquery (Jquery.create ("#su"). Click ());

Console.WriteLine (ie. Executejquery<string> (Jquery.create ("' title '"). Text ()));

This valid code does not exceed 10 lines of code snippet, simulates the navigation and waits for the Baidu page, then simulates the user search, the output title function,

On the window, we have a webBrowser control called WebBrowser and a TextBox control called Txtkeyword.

At the beginning of the code, we first determine whether the user input keyword is empty, if it is empty direct return, and keywords used in Baidu search.

After that, we used Iebrowser 's Navigate and Ieflow 's Wait method to navigate and wait for the Baidu page to be loaded. The wait method is used to allow the program to wait until a condition is established before continuing to execute downward, while the code we specify is a urlcondition, which is a page condition, Stringcomparemode is set to Startwith for the wait address to "http:// www.baidu.com/"Start Page loading.

When the page loads, we execute the iebrowser Installjquery method to install the JQuery script for the page. And the Installjquery method has two forms, one can load the string as a script, the other is to pass a URI as a parameter, the URI is a network address or a local file address, in the example, we use the first form, the class JQuery static properties Codemi n returns the compressed script for JQuery.

Next , we re-list two lines of important code, in which we pass the JQuery class as a parameter to Iebrowser 's Executejquery method to execute on the WebBrowser page JQuery script.

Ie. Executejquery (Jquery.create ("#kw"). Val ("'" + this.txtKeyword.Text.Trim () + "'"));

Ie. Executejquery (Jquery.create ("#su"). Click ());

We can create an instance of jquery using the static method of jquery, or new jquery. The jquery script for the first line corresponds to $ (' #kw '). Val (' < user input keywords > '), while the second line of jquery objects corresponds to $ (' #su '). Click ();

In this case, jquery in the jquery class and the actual JS script are almost identical in usage, except that Create represents $, and the members of jquery are capitalized in the jquery class, but the member names do not change. The jquery class is used to build jquery scripts in. NET and pass them as parameters to iebrowser execution.

In fact, you can also use the Iebrowser executescript to directly execute JQuery script, the effect is exactly the same.

And the meaning of the two jQuery, that the user entered the keyword into the search box in the page, and then click the Search button, kw and Su are Baidu page search box and search button ID, ' #kw ' means to take the ID of the page element of KW, these are designed to JQuery The content of itself, we can refer to http://jquery.com.

code to here is finished, you need to see the actual operation of the results, you can refer to the following demonstration.

Iebrowser is open source shared code, can download DLL or source code on http://code.google.com/p/zsharedcode/wiki/Download page

Alternate download Link: iebrowser.2.5.0.debug.noparam.zip

Bill: WebBrowser Execute and install jquery script (Iebrowse)

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.