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?

1
  • 2
    find_element_by_xpath('//a[contains(@href,"22")]') Commented Feb 20, 2016 at 0:19

3 Answers 3

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")]')

Sign up to request clarification or add additional context in comments.

Comments

2

Try this one:

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

1 Comment

ends-with() is not gonna work - it is a part of XPath 2.0.
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]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.