Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force NHTMLUNIT to Ignore Page JavaScript errors and Continue Script Execution?

Tags:

c#

ajax

asp.net

I am part of ASP.NET and C# project. We are trying to make our asp.net portal Google search engine friendly (https://developers.google.com/webmasters/ajax-crawling/). Web pages in our site are generated dynamically and the DOM is modified with JavaScript so we use NHTML to generate the snapshot (Server-side) when the Google search engine sends the request. It generates the HTML snapshot but the issue is when there is a script error in the page, it returns partially rendered page (the content that gets modified by the page JavaScript is partially rendered). Pages work perfectly in Browsers.

I tried the following options

ThrowExceptionOnScriptError = false,
ThrowExceptionOnFailingStatusCode = false

But no LUCK.

Is there a way to Force NHtmlUnit to ignore page errors and continue execution?

following is the code

    // Create a webclient.
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17)
        {
            ThrowExceptionOnScriptError = false,
            ThrowExceptionOnFailingStatusCode = false
        };

    webClient.WaitForBackgroundJavaScript(5000);

    // Load the Page with the given URL.
    HtmlPage htmlPage = webClient.GetHtmlPage(url);

    // Return the page for the given URL as Text.
    return htmlPage.WebResponse.ContentAsString;
like image 976
RAM Avatar asked Apr 12 '13 11:04

RAM


1 Answers

// Create a webclient.
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17)
    {
        JavaScriptEnabled = true
        ThrowExceptionOnScriptError = false,
        ThrowExceptionOnFailingStatusCode = false,
    };

webClient.WaitForBackgroundJavaScript(5000);

HtmlPage htmlPage = webClient.GetHtmlPage(url);

// Return the page for the given URL as Text.
return htmlPage.WebResponse.ContentAsString;

I noticed you didn't enable JavaScript, sorry if I'm wrong.

like image 126
vfioox Avatar answered Oct 05 '22 04:10

vfioox