1

I am searching element like find_element_by_xpath('//a[@href="/requirements/22"]')

Can I search with only part of it. I mean instead of /requirements/22 something like *22?

user2661518
  • 2,485
  • 7
  • 34
  • 71

3 Answers3

2

Try this one:

find_element_by_xpath('//a[ends-with(@href, "22")]')
Andersson
  • 49,746
  • 15
  • 64
  • 117
1

Contains :

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

Starts With:

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

Ends With:

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

You are also free to try combinations, Example: find_element_by_xpath('//a[starts-with(@href, "22") and contains(@href, "req")]')

Prateek
  • 1,135
  • 1
  • 8
  • 21
1

You would think of using ends-with(), but it is a part of 2.0 and is not going to work in your case.

Either use contains():

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

Or, use ends-with CSS selector:

find_element_by_css_selector('a[href$=22]')
Community
  • 1
  • 1
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148