是否有一种方法可以强制NHTMLUNIT忽略页面JavaScript错误并继续执行脚本?

本文关键字:JavaScript 错误 脚本 执行 继续 NHTMLUNIT 一种 方法 是否 | 更新日期: 2023-09-27 18:08:54

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

我尝试了以下选项

ThrowExceptionOnScriptError = false,
ThrowExceptionOnFailingStatusCode = false

But no LUCK.

是否有办法强制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;

是否有一种方法可以强制NHTMLUNIT忽略页面JavaScript错误并继续执行脚本?

// 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,对不起,如果我错了。