【问题标题】:Is there a way to force NHTMLUNIT to Ignore Page JavaScript errors and Continue Script Execution?有没有办法强制 NHTMLUNIT 忽略页面 JavaScript 错误并继续执行脚本?
【发布时间】:2013-04-12 11:08:56
【问题描述】:

我是 ASP.NET 和 C# 项目的一部分。我们正在努力使我们的 asp.net 门户网站 Google 搜索引擎友好 (https://developers.google.com/webmasters/ajax-crawling/)。我们网站中的网页是动态生成的,DOM 是用 JavaScript 修改的,所以当 Google 搜索引擎发送请求时,我们使用 NHTML 来生成快照(服务器端)。它会生成 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;

【问题讨论】:

    标签: c# asp.net ajax


    【解决方案1】:
    // 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,如果我错了,请见谅。

    【讨论】:

    • 我不知道@RAM 的情况,但我启用了我的。问题是,我不确定这是 [N]HtmlUnit 问题而不是 jQuery 问题,因为它在无头浏览器中运行。它可能取决于 HtmlUnit 没有提供的东西(比如窗口对象,或者浏览器本身提供的其他东西)。
    • 是的,我启用了 JavaScript。页面中的 JavaScript 代码引发异常,导致 NHTMLUNIT 停止执行,因此页面被部分呈现。相同的 JavaScript 在浏览器中工作并呈现完整的页面。
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多