gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:47:54 25 4
gpt4 key购买 nike

我是 ASP.NET 和 C# 项目的一部分。我们正在努力使我们的 asp.net 门户网站对 Google 搜索引擎友好 (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;

最佳答案

// 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,如果我错了,抱歉。

关于c# - 有没有办法强制 NHTMLUNIT 忽略页面 JavaScript 错误并继续执行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com