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.
asked Jun 29, 2016 at 9:14
Mahammad Adil Azeem
9,42215 gold badges62 silver badges88 bronze badges
1 Answer 1
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')]
Mahammad Adil Azeem
9,42215 gold badges62 silver badges88 bronze badges
answered Jun 29, 2016 at 9:36
noor
2,9942 gold badges20 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Mahammad Adil Azeem
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.
Mahammad Adil Azeem
It didn't worked out. Gonna try the upper comment. Please upvote the question as well so some one else might answer it.
noor
@AdilMalik have u succedded?
Mahammad Adil Azeem
Yes. Thanks Guys. So the point is we can use patterns in xpath ?
noor
Let us continue this discussion in chat.
default
.//*[starts-with(@id, 'accordion-note')]