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')]
    – splash58
    CommentedJun 29, 2016 at 9:40
  • 2
    I don't know what version i am using ? @splash58 ..... How to find out ?CommentedJun 29, 2016 at 9:42
  • i dom't know about selenium too. If the answer below will return error, try my expression
    – splash58
    CommentedJun 29, 2016 at 9:43
  • 1
    @splash58 your answer worked very well. Below noor has edited his answer as well.CommentedJun 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')] 
5
  • 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.CommentedJun 29, 2016 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.CommentedJun 29, 2016 at 10:38
  • @AdilMalik have u succedded?
    – noor
    CommentedJun 29, 2016 at 11:04
  • Yes. Thanks Guys. So the point is we can use patterns in xpath ?CommentedJun 29, 2016 at 11:09
  • Let us continue this discussion in chat.
    – noor
    CommentedJun 29, 2016 at 11:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.