【问题标题】:How to find all elements that contains a value in a specific attribute using selenium python如何使用 selenium python 查找包含特定属性中的值的所有元素
【发布时间】:2021-09-15 13:23:37
【问题描述】:

您好,我正在尝试查找其 style 中包含 display: none 的所有元素 属性。例如在这个 div 中:

<div class="w3-col w3-border" id="imgbox1" style="width: 30%; display: none;"><p style="word-wrap: break-word;"><code class="w3-codespan" style="wrap">display:none</code></p>
  <img src="img_5terre.jpg" style="width:100%" alt="Italy">
  <p><button class="ws-btn w3-block" onclick="removeElement()">Remove</button></p>
</div>

我怎样才能做到这一点?

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    下面的xpath

    //*[contains(@style,'display: none')]
    

    应该代表所有样式为display none的节点。

    对于这个特定的div,您可以将//* 替换为//div。像这样:

    //div[contains(@style,'display: none')]
    

    示例代码:

    disp_none = driver.find_element_by_xpath("//*[contains(@style,'display: none')]")
    

    上面的列表disp_none 应该包含所有的网络元素。

    像这样打印尺寸:

    print(len(disp_none))
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2021-08-07
      相关资源
      最近更新 更多