使用xpath选择包含id和class的href

时间:2013-12-01 17:25:49

标签: xpath

假设我有这样的DOM:

<div id="tabsmenu">
    <ul>
        <li class="one"><a href="'#foo" class="ui-tabs-anchor" id="tab1"></a>foo</li>
        <li class="two"><a href="#baz" class="ui-tabs-anchor" id="tab2">baz</a> </li>
    </ul>
</div>

我想从<a href>元素中获取文字:

# desired output: ['#foo', '#baz']

如何使用xpath并使用组合idclass内的特定id元素?

已经尝试过:

 some_doc.xpath('//a[@id="tabsmenu"]/[@class="ui-tabs-anchor"]/@href')

 # select all href tags of any a element that is in id tabsmenu and class attribute ui-   tabs-anchor

编辑 - 将tabmenu更正为tabsmenu

1 个答案:

答案 0 :(得分:2)

你最有可能找到这样的东西:

//div[@id='tabsmenu']//a[@class='ui-tabs-anchor']/@href

这将获得属于href标记的所有a属性,其中包含类ui-tabs-anchor,并且位于div元素内且标识为tabsmenu的属性。

另外,您可能想看一下这个问题:

Find out if class name contains certain text

这是因为该类将匹配确切的值(ui-tabs-anchor),并且可能会添加一些其他类,例如class="ui-tabs-anchor disabled",然后就不会有匹配。

相关问题