find multiple elements by id

Issue

I am trying to find multiple elements by their respective ids
The elements are named

dcm-reservation-limit-multiple-input-generic-X

with X being the number of elements, it could be:

dcm-reservation-limit-multiple-input-generic-0
dcm-reservation-limit-multiple-input-generic-1
dcm-reservation-limit-multiple-input-generic-2
...
dcm-reservation-limit-multiple-input-generic-10

and the list goes on.
I’ve tried already this code:

pricesToFind = driver.find_elements_by_xpath(".//*[@id='dcm-reservation-limit-multiple-input-generic-']")

without any success, the results always return NULL even if they really exist

piece of HTML to help:

<input _ngcontent-ihk-c34="" autocomplete="off" class="lib-input" maxlength="11" type="text" id="dcm-reservation-limit-multiple-input-variation-0">

the input name is randomly generated by javascript, so it’s useless to scrape it via input name

Solution

Use contains or starts-with?

pricesToFind = driver.find_elements_by_xpath(".//*[contains(@id,'dcm-reservation-limit-multiple-input-generic-')]")

pricesToFind = driver.find_elements_by_xpath(".//*[starts-with(@id,'dcm-reservation-limit-multiple-input-generic-')]")

Answered By – Yevhen B

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published