Skip to main content
 首页 » 编程设计

python之如何在python中使用selenium查找某个元素是否存在于一类html中

2024年10月24日12bluestorm

我必须对同一类型进行划分:

1) 其中包含特定类的划分 (tile-freeCancellation)

<p class="col col-md-6 tile-duration"> 
  <span id="activityDuration183909" class="tile-activity-duration"> 
    <span class="icon icon-time" aria-hidden="true"></span> 
    <span class="alt">Duration</span> 
    2d+  
  </span> 
  <span id="activityFreeCancellation183909" class="tile-freeCancellation"> 
    <span id="offerFreeCancellationIcon4" class="icon icon-success" aria-hidden="true"></span> 
    <span id="offerFreeCancellationText4" class="tile-free-cancel"> Free cancellation </span> 
  </span> 
</p> 

2)没有那个类(tile-freeCancellation)

<p class="col col-md-6 tile-duration"> 
  <span id="activityDuration186022" class="tile-activity-duration"> 
    <span class="icon icon-time" aria-hidden="true"></span> 
    <span class="alt">Duration</span> 
    2d  
  </span> 
</p> 

有什么方法可以检查类的元素 (tile-freeCancellation) 是否存在于主元素(类 tile-duration)中?

我在这个程序中使用 python3,并使用函数 find_element_by_class_name 来查找主类的元素

请您参考如下方法:

我的尝试 - 三元运算(if)或 if 或 @W 的辅助函数。赛博斯基

if len(driver.find_elements_by_class_name("tile-freeCancellation"))>0: 
    #do something 
else: 
    # do another 

如果是嵌套元素,例如选择一个 div(母元素),然后在其中搜索。

if len(motherelement.find_elements_by_class_name("tile-freeCancellation"))>0: 
    #do something 
else: 
    # do another