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" 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
CodePudding user response:
I personally have never used Selenium a lot, but I hypothetically know how to do this. I would use a while loop, and then run it over and over again. (Just letting you know, I have no way to actually test this, as I couldn't find any elements in any website that have those same conditions)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get("somerandomwebsitehere.com/index.html")
a = 1
while a <= 100:
element = browser.find_element(By.ID, "dcm-reservation-limit-multiple-input-generic-" str(a))
# Do whatever you want to the element
a = int(a) 1
Hopefully this helps. I make no guarantees that this code will work, but now you have an idea on how to make this happen.
CodePudding user response:
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-')]")