带有部分字符串的xpath搜索属性

时间:2016-02-20 00:15:38

标签: python xpath selenium-webdriver

我正在搜索find_element_by_xpath('//a[@href="/requirements/22"]')

之类的元素

我可以只搜索其中的一部分。我的意思是代替/requirements/22而不是*22

3 个答案:

答案 0 :(得分:2)

试试这个:

find_element_by_xpath('//a[ends-with(@href, "22")]')

答案 1 :(得分:1)

包含:

find_element_by_xpath('//a[contains(@href, "22")]')

开头时间:

find_element_by_xpath('//a[starts-with(@href, "22")]')

结束于:

find_element_by_xpath('//a[starts-with(@href, "22")]')

你也可以自由尝试组合,例如: find_element_by_xpath(' // a [starts-with(@href," 22")并包含(@href," req")]')

答案 2 :(得分:1)

您会考虑使用ends-with(),但它是part of 2.0,并不适用于您的情况。

使用contains()

find_element_by_xpath('//a[contains(@href, "22")]')

或者,使用结束 - 使用CSS选择器

find_element_by_css_selector('a[href$=22]')