Html Agility Pack cannot find list option using xpath
This is related to my previous q开发者_StackOverflow社区uestion, but it seems I have another corner case where Html Agility Pack doesn't work as expected.
Here's the Html (stripped down to the essentials, and sensitive information removed):
<html>
<select id="one-time-payment-form:vendor-select-supplier">
<option value="1848">Frarma Express</option>
<option value="2119">Maderas Garcia</option>
<option value="1974">Miaris, S.A.</option>
<option value="3063">Ricoh Panama</option>
<option value="3840">UNO EXPRESS</option>
<option value="68">Garrett Blaser Gretsch</option>
<option value="102">Oriel Antonio Grau</option>
</select>
</html>
And here's the code:
const string xpath = "//*[contains(@id, 'one-time-payment-form:vendor-select-')]/option[contains(text(), 'UNO EXPRESS')]";
var driver = new FirefoxDriver(new FirefoxProfile()) { Url = "PATH_TO_FILE_CONTAINING_HTML_SHOWN_ABOVE" };
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 };
HtmlNode.ElementsFlags.Remove("form");
htmlDoc.LoadHtml(source);
var nodes = htmlDoc.DocumentNode.SelectNodes(xpath);
Console.WriteLine(nodes!=null ? "Html Agility Pack success" : "Html Agility Pack failure");
driver.Quit();
When I run the code, the console reads:
WebDriver success
Html Agility Pack failure
So clearly WebDriver has no problem locating the item @XPath //*[contains(@id, 'one-time-payment-form:vendor-select-')]/option[contains(text(), 'UNO EXPRESS')]
, but Html Agility Pack cannot.
Any ideas?
This is "by design". It's the same idea for OPTION and FORM. Some tags are handled differently because of historical reasons by the Html Agility Pack. Back then in HTML 3.2 time, OPTION was not always closed, and in HTML 3.2, it's not required.
Try adding this:
HtmlNode.ElementsFlags.Remove("option");
精彩评论