WebClient runs javascript

≡放荡痞女 提交于 2019-12-28 02:53:22

问题


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_DocumentCompleted

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?


回答1:


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.



来源:https://stackoverflow.com/questions/5372277/webclient-runs-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!