开发者

WebClient runs javascript

开发者 https://www.devze.com 2023-02-17 20:39 出处:网络
I have one. aspx page that has some JavaScript functions that control paging. I can run this javascript function via webbrowser with the following method within the WebBrowser1_DocumentComplete开发者

I have one. aspx page that has some JavaScript functions that control paging.

I can run this javascript function via webbrowser with the following method within the WebBrowser1_DocumentComplete开发者_Python百科d

WebBrowser1.Document.Window.DomWindow.execscript ("somefunction();", "JavaScript")

The webbrowser is very slow and I prefer to use System.Net.WebClient.DownloadString.

Has some way to run this script with the System.Net.WebClient methods that are faster, or some other way?


Well, no. WebClient is an HTTP client, not a web browser.

An HTTP client follows the HTTP spec; the fact that your HTTP requests result in HTML is irrelevant to the client.

A web browser, on the other hand, in addition to being an HTTP client, also knows how to parse HTML responses (and execute JavaScript, etc.).

It seems that what you are looking for is called a "headless browser", which supports loading HTML and running JavaScript on the DOM, exactly like you need. Headless browsers are also generally quite fast compared to normal browsers, since they don't need to do any rendering.

There are several headless browsers. HtmlUnit (which can be converted to run on .NET) seems like a good choice, as does envjs (it's written in JavaScript, which can be embedded in .NET). Unfortunately, I have no experience with either, but they both look super-cool, especially envjs. Update: a nice, more up to date list of headless browsers has been published on GitHub.

There are also other alternatives to the WebBrowser control which may or may not be faster in your case, if you want to stay with a control.

0

精彩评论

暂无评论...
验证码 换一张
取 消