有没有办法强制NHTMLUNIT忽略页面JavaScript错误并继续执行脚本?

时间:2013-04-12 11:08:57

标签: c# asp.net ajax

我是ASP.NET和C#项目的一部分。我们正在努力使我们的asp.net门户网站谷歌搜索引擎友好(https://developers.google.com/webmasters/ajax-crawling/)。我们网站中的网页是动态生成的,DOM是使用JavaScript修改的,因此我们使用NHTML在Google搜索引擎发送请求时生成快照(服务器端)。它会生成HTML快照,但问题是当页面中出现脚本错误时,它会返回部分呈现的页面(部分呈现页面JavaScript修改的内容)。页面在浏览器中完美运行。

我尝试了以下选项

ThrowExceptionOnScriptError = false,
ThrowExceptionOnFailingStatusCode = false

但没有运气。

有没有办法强制NHtmlUnit忽略页面错误并继续执行?

以下是代码

    // 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;

1 个答案:

答案 0 :(得分:5)

// 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;

我注意到你没有启用JavaScript,抱歉,如果我错了。