Webclient Runs JavaScript

Invoke a tag javascript click function from Webclient

You cannot execute javascript from WebClient. You can simulate such request and achieve the required response from the server but it takes some reverse-engineering.

To understand how to get the desired response from the server first you have to perform this operation through browser and record request that is generated by clicking on that link (for that you can use Fiddler Web Debugger). Than you have to recreate such request from your WebClient, that is - send all the required data to the server properly formatted, along with cookies and correct request type (GET or POST - synchronous or asynchronous).

Why you cannot use WebClient for javascript execution is nicely described here

And how you can create a request that resembles the one created by javascript click can be deduced from this.

HtmlUnit How to get a page after executing JavaScript

Yes you are right, the method getNewPage() was removed with the version 2.33 as part of a a huge refactoring regarding event handling.

As replacement you can use the enclosed page of the window.

E.g. if you are sure that the resulting page is in the same window as your current page (has replaced your page without opening a new window), you can use

page.getEnclosingWindow().getEnclosedPage()

If your code might opend up a new window it will be more save to ask the webClient for the current window (newly opened windows are automatically marked as the current)

page.getWebClient().getCurrentWindow().getEnclosedPage()

Or in your code sample you can directly use the client

webClient.getCurrentWindow().getEnclosedPage()

Hope that helps.

c# get content loaded by js from url

WebClient and DownloadString will just download the content of the page as string, to be able to run the JavaScript code on this page, you will need an object which understand DOM and can run JavaScript, this will be a browser.

Some more information about this subject:

Running Scripts in HtmlAgilityPack

Load a DOM and Execute javascript, server side, with .Net



Related Topics



Leave a reply



Submit