【发布时间】:2017-01-19 17:26:38
【问题描述】:
我无法为我正在编写的程序找到正确的 XPath。 XML 是这样的:
<root>
<div class"XYZ">
<div>
<div class = "abc">
<a class = "important">Important Information - 1</a>
</div>
<div class = "abc">
<a class = "important">Important Information - 2</a>
</div>
<div class = "abc">
<a class = "important">Important Information - 3</a>
</div>
</div>
</div>
</root>
所以,如您所见,有一个类我需要转到“XYZ”,并且在该类中有许多名为“abc”的类。实际上,“abc”类前面还有更多<div>。
我需要获取所有“重要信息 - X”。
我的想法是
FindElements(By.XPath("//*[contains(@class,'XYZ')]/*[contains(@class,'important')]");
我不能只搜索 class= "important" 本身的问题在于,文档中有另一部分具有相同的类名,但我不需要该信息。只有“XYZ”类中的那些。
当我只使用时:
FindElements(By.XPath("//*[contains(@class,'XYZ')]");
它有效,但我也得到了很多不重要的信息,因为<div>s 中有更多内容。
【问题讨论】:
-
把中间的
/*换成//*? -
该死......它工作xD
-
发帖的专业提示:一旦您得到问题的答案,请不要编辑它来询问另一个问题。我们需要确保编辑不会使已经给出的答案无效,所以提出一个新问题,如果合适,请在此处链接。其次,我们在这里不使用 [已解决的] 标题黑客 - 应该使用接受/勾选系统。最后,请尽量避免喋喋不休的风格——技术问题不是“更戏剧化”。谢谢!
-
好吧,在我的辩护中,我想写“问题”,但它似乎被列入黑名单......迫使我更有创意