WebDriver can find element using xpath, Html Agility Pack cannot

I constantly had problems with the Html Agility Pack; my XPath queries only work when they are very simple:

//*[@id='some_id']

or

//input

However, at any time when they become more complex, the Html Agility Pack cannot handle this. Here is an example that demonstrates the problem: I use WebDriver to go to Google and return the source of the page that is passed to the Html Agility Pack, and both WebDriver and HtmlAgilityPack try to find the / node element (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 ? "Html Agility Pack success" : "Html Agility Pack failure");

driver.Quit();

In this case, WebDriver successfully placed the item, but the Html Agility Pack did not.

, , xpath , : //input [@name= 'q'], , , -, , , xpath WebDriver FirePath FireFinder Firefox.

WebDriver , Html Agility Pack ?

+3
1

, , FORM. HTML Agility Pack - - , .

, , :

.//div/div[2]/table/tr/td/table/tr/td/div/table/tr/td/div/div[2]/input

, , :

.//form/div/div[2]/table/tr/td/table/tr/td/div/table/tr/td/div/div[2]/input

. HTML, :

HtmlNode.ElementsFlags.Remove("form");
+8

All Articles