Use WebBrowser to perform Code operations on webpages

Source: Internet
Author: User

Review the previous article and provide two methods to automate webpage operations.

  • Tamper with HTML code and load it to WebBrowser for running
  • Use Fiddle to intercept the URL and data sent to the server, and use the HttpRequest class code to POST to the server.

Another method provided today is to use the WebBrowser control in the WinForm control to operate the page.

Here we will simulate a search process, open the Baidu website, enter the search keyword, and open the connection in the search results.

First, create a Windows Form Application Project and pull a WebBrowser control on the interface.

The URL of WebBrowser1 is written as the address of Baidu.

And set the WebBrowser events after loading the webpage.

Note that some classes that operate on HTML nodes are used in this project. You need to reference the two DLL files.

Then, compile the WebBrowser1 address switching completion event in the background.

public Form1()        {            InitializeComponent();            (webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 +=                 new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(Form1_NavigateComplete2);        }         void Form1_NavigateComplete2(object pDisp, ref object URL)        {            IHTMLDocument2 doc = (webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).Document as IHTMLDocument2;            doc.parentWindow.execScript("window.alert=null", "javascript");            doc.parentWindow.execScript("window.confirm=null", "javascript");            doc.parentWindow.execScript("window.open=null", "javascript");            doc.parentWindow.execScript("window.showModalDialog=null", "javascript");            doc.parentWindow.execScript("window.close=null", "javascript");        }

Of course, there are also events after the document is loaded. Here, you need to know the search box ID and the Search button ID on the Baidu homepage to facilitate keyword filling and search. We checked the source code of the Baidu homepage and found a Form for submitting the search.

 
In the code, we get the kw and enter the keyword "Weng Yuli". Then, click the su button.
Private void webbrowserincludocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {HtmlDocument doc = this. webBrowser1.Document; doc. getElementById ("kw "). innerText = "Weng Yuli"; doc. getElementById ("su "). invokeMember ("click ");}

The code is written here and submitted for search. WebBrowser will also receive the record after this submission. Next we need to open a connection in the result to do the experiment. First, check the HTML source code of the search result.

Private void webbrowserincludocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {HtmlDocument doc = this. webBrowser1.Document; if (doc. url. absoluteUri = "http://www.baidu.com/") {doc. getElementById ("kw "). innerText = "Weng Yuli"; doc. getElementById ("su "). invokeMember ("click");} if (doc. url. absoluteUri. contains ("wd") // according to {HtmlElement table = doc in the URL. getElementById ("1"); // locate the URL of the first search result based on the HTML table of the search result. var html = table is simulated here. children [0]. children [0]. innerHtml; var url = html. substring (html. indexOf ("href") + 6, html. indexOf ("target"); this. webBrowser1.Navigate (url );}}

In this way, you can automatically and periodically run this APP to operate the page. Is it better to POST data to the server than to use HttpRequest? There is no cookie or script encryption algorithm.

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.