我一直遇到Html Agility Pack的问题; 我的XPath查询只有在非常简单时才能工作:
//*[@id='some_id']
Run Code Online (Sandbox Code Playgroud)
要么
//input
Run Code Online (Sandbox Code Playgroud)
但是,只要它们变得更复杂,那么Html Agility Pack就无法处理它.这是一个演示问题的示例,我使用WebDriver导航到Google,并返回页面源,传递给Html Agility Pack,WebDriver和HtmlAgilityPack都尝试定位元素/节点(C#):
//The XPath query
const string xpath = "//form//tr[1]/td[1]//input[@name='q']";
//Navigate to Google and get page source
var driver = new FirefoxDriver(new FirefoxProfile()) { Url = "http://www.google.com" };
Thread.Sleep(2000);
//Can WebDriver find it?
var e = driver.FindElementByXPath(xpath);
Console.WriteLine(e!=null ? "Webdriver success" : "Webdriver failure");
//Can Html Agility Pack find it?
var source = driver.PageSource;
var htmlDoc = new HtmlDocument { OptionFixNestedTags = true };
htmlDoc.LoadHtml(source);
var nodes = htmlDoc.DocumentNode.SelectNodes(xpath);
Console.WriteLine(nodes!=null …Run Code Online (Sandbox Code Playgroud)