3

I am working with selenium python and lettuce to test django application.

There are many elements having xpath in the following pattern and i don't know how many of these elements exists in the document.

.//*[@id='accordion-note-1']
.//*[@id='accordion-note-2']
.//*[@id='accordion-note-3']
.//*[@id='accordion-note-4']

Is there any way to use pattern in driver.find_elements_by_xpath ?
Basically my purpose is to get all items having this pattern of xpath.

4
  • 1
    If xpath 1.0 - .//*[starts-with(@id, 'accordion-note')] Commented Jun 29, 2016 at 9:40
  • 2
    I don't know what version i am using ? @splash58 ..... How to find out ? Commented Jun 29, 2016 at 9:42
  • i dom't know about selenium too. If the answer below will return error, try my expression Commented Jun 29, 2016 at 9:43
  • 1
    @splash58 your answer worked very well. Below noor has edited his answer as well. Commented Jun 29, 2016 at 11:07

1 Answer 1

7

Not sure this will work, but you can try by using the below xpath:

.//*[starts-with(@id, 'accordion-note')]

here is the link http://www.zvon.org/xxl/XSLTreference/Output/function_starts-with.html

u can also use contain here like

.//*[contains(@id, 'accordion-note')]     
Sign up to request clarification or add additional context in comments.

5 Comments

I have used your answer like this. world.driver.find_elements_by_xpath(".//*[matches(@id, 'accordion - IT - \\d+')]"). And this is the error i got this error. The given selector .//*[matches(@id, 'accordion - IT - \d+')] is either invalid or does not result in a WebElement.
It didn't worked out. Gonna try the upper comment. Please upvote the question as well so some one else might answer it.
@AdilMalik have u succedded?
Yes. Thanks Guys. So the point is we can use patterns in xpath ?

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.