Html Agility Pack cannot find list option using xpath

非 Y 不嫁゛ 提交于 2019-12-07 19:24:12

问题


This is related to my previous question, 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?


回答1:


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");


来源:https://stackoverflow.com/questions/6230142/html-agility-pack-cannot-find-list-option-using-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!