我正在尝试逐步浏览HTML Agility Pack网站上的示例,以学习如何使用它。但是,此示例似乎无法正常工作,我希望获得一些帮助来弄清原因:https://html-agility-pack.net/from-browser
我的代码:
const raw = `[[24297, 'CWCS Sump 002', -121.50842, 38.54798, '2', 'SEWER SUMP'],[8035, 'CWCS Sump 002A (56)', -121.50842, 38.54798, '2A', 'SEWER SUMP'],[9334, 'CWCS Sump 001', -121.5110297, 38.5703431, '1', 'SEWER SUMP']]`;
const res = JSON.parse(raw.replace(/(?<!\\)'/g, `"`));
console.log(res);
我收到的错误是:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getWebsite();
}
private void getWebsite()
{
string url = "http://html-agility-pack/from-browser";
var web1 = new HtmlWeb();
var doc1 = web1.LoadFromBrowser(url, o =>
{
var webBrowser = (WebBrowser)o;
// WAIT until the dynamic text is set
return !string.IsNullOrEmpty(webBrowser.Document.GetElementById("uiDynamicText").InnerText);
});
var t1 = doc1.DocumentNode.SelectSingleNode("//div[@id='uiDynamicText']").InnerText;
var web2 = new HtmlWeb();
var doc2 = web2.LoadFromBrowser(url, html =>
{
// WAIT until the dynamic text is set
return !html.Contains("<div id=\"uiDynamicText\"></div>");
});
var t2 = doc2.DocumentNode.SelectSingleNode("//div[@id='uiDynamicText']").InnerText;
}
}
具体来说,错误显示在此行上:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.HtmlDocument.GetElementById(...) returned null.
我仔细检查了一下,据我所知,我的代码与示例相同。我在哪里错了?