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.

Mahammad Adil Azeem
  • 9,112
  • 13
  • 57
  • 84

1 Answers1

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')]     
Mahammad Adil Azeem
  • 9,112
  • 13
  • 57
  • 84
noor
  • 2,954
  • 2
  • 16
  • 29
  • 2
    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 Jun 29 '16 at 10:35
  • 1
    It didn't worked out. Gonna try the upper comment. Please upvote the question as well so some one else might answer it. – Mahammad Adil Azeem Jun 29 '16 at 10:38
  • @AdilMalik have u succedded? – noor Jun 29 '16 at 11:04
  • Yes. Thanks Guys. So the point is we can use patterns in xpath ? – Mahammad Adil Azeem Jun 29 '16 at 11:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115952/discussion-between-noor-and-adil-malik). – noor Jun 29 '16 at 11:09