我是 ASP.NET 和 C# 项目的一部分。我们正在努力使我们的 asp.net 门户对 Google 搜索引擎友好(https://developers.google.com/webmasters/ajax-crawling/ 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;
// 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,如果我错了,抱歉。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)