通过ID找到多个元素

发布于 2025-02-13 01:36:39 字数 761 浏览 2 评论 0原文

我试图通过其各自的ID找到多个元素 元素命名

dcm保留限制限制 - 元素输入 - X

,x是元素的数量,可能是:

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

列表还在继续。 我已经尝试过此代码:

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

没有任何成功,结果始终返回null,即使它们确实存在

HTML以帮助:

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

输入名称是由JavaScript随机生成的,因此通过输入名称刮擦它是毫无用处的。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2025-02-20 01:36:39

使用包含 starter

pricestofind = driver.find_elements_by_xpath(“ .///* [contains(@ID,'dcm-rimit-mimit-multiple-multiple-input-generic-generic--')]”)

pricestofind = driver.find_elements_by_xpath(“ .///* [starting-with(@ID,'dcm-kerveration-limit-multiple-multiple-input-generic - ')]”)

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-')]")

心安伴我暖 2025-02-20 01:36:39

我个人从来没有经常使用硒,但是我假设地知道该怎么做。我会使用一段时间循环,然后一遍又一遍地运行它。 (只是让您知道,我无法实际测试这一点,因为我在任何具有相同条件的网站中都找不到任何元素)

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

希望这会有所帮助。我不能保证此代码会起作用,但是现在您对如何实现这一目标有了一个想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文