Xpath:如果包含特定单词,则获取href

时间:2017-04-24 09:54:25

标签: python regex xpath href

设置

我正在使用以下xpath

从页面中提取href
@FindBy(id=xxx)

给了我一个看起来像hrefs的列表,

'/html/body/div/div[2]/div[2]/div/div/p[1]/a/@href'

<小时/> 的问题

我对['#', 'showv2.php?p=Glasgow City&t=Anderston', 'showv2.php?p=Glasgow City&t=Anniesland', 'showv2.php?p=Glasgow City&t=Ashfield', '#', 'showv2.php?p=Glasgow City&t=Baillieston', ⋮ 'showv2.php?p=Glasgow City&t=Yoker'] href不感兴趣。我感兴趣的所有href都包含'#'。如何仅选择包含Glasgow的href?

我已经看到有关Glasgow等正则表达式的答案,但没有使用href。这些答案似乎不适用于href。

我已经看到有关href的开头或结尾的正则表达式的答案,但我希望能够在'包含'一个单词时使用正则表达式。

1 个答案:

答案 0 :(得分:3)

使用contains(@href, 'Glasgow')&#34;限制&#34;在a元素上:

'/html/body/div/div[2]/div[2]/div/div/p[1]/a[contains(@href, "Glasgow")]/@href'

然后,它只会在<a>属性值中包含Glasgow的指定路径下找到href个。{/ p>