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,677
  • 9
  • 42
  • 79

3 Answers3

2

Try this one:

find_element_by_xpath('//a[ends-with(@href, "22")]')
Andersson
  • 51,635
  • 17
  • 77
  • 129
2

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,145
  • 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
  • 462,703
  • 120
  • 1,088
  • 1,195