如何在Python Selenium Web Driver中获取Div属性文本

发布于 2025-02-13 22:55:32 字数 454 浏览 3 评论 0原文

希望一切都很好。我想获得“数据ID”属性文本。请任何人帮助我在这里得到这一点。提前致谢。

<div class="_1AtVbE col-12-12">
  <div class="_13oc-S">
    <div data-id="MOBG6WQWRGRRDBH6" style="width: 100%;">
      <div class="_2kHMtA" data-tkid="82daf062-714d-464f-b670-ce7681250431.MOBG6WQWRGRRDBH6.SEARCH">
      </div>
    </div>
  </div>
</div>

Hope all are doing well. I want to get "data-id" property text. Please anyone help me here to get that. Thanks in advance.

<div class="_1AtVbE col-12-12">
  <div class="_13oc-S">
    <div data-id="MOBG6WQWRGRRDBH6" style="width: 100%;">
      <div class="_2kHMtA" data-tkid="82daf062-714d-464f-b670-ce7681250431.MOBG6WQWRGRRDBH6.SEARCH">
      </div>
    </div>
  </div>
</div>

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

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

发布评论

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

评论(2

十二 2025-02-20 22:55:32

使用CSS选择器查找具有数据ID属性的DIV元素

driver.find_element('css selector', 'div[data-id]').getAttribute('data-id')

注意:由于您的HTML样本很小,因此该代码可以工作。在更大的样本上,缩小到目标div元素的父,或使用find_elements而不是要过滤您想要的元素。

参考阅读:所有CSS属性选择器

Use CSS selector to find div element with data-id attribute

driver.find_element('css selector', 'div[data-id]').getAttribute('data-id')

Note: since your HTML sample is small this code would work. On a bigger sample, narrow down to the parent of your target div element, or use find_elements instead and filter for the element you want.

Reference reading: All CSS Attribute Selectors

°如果伤别离去 2025-02-20 22:55:32
String dataId = driver.FindElement(By.xpath("//div[@class='_2kHMtA']/..")).getAtribute("data-id");
String dataId = driver.FindElement(By.xpath("//div[@class='_2kHMtA']/..")).getAtribute("data-id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文