python - 如何在python中使用selenium查找html类中是否存在元素

标签 python selenium webdriver

我必须有相同的划分类型:

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)或@W的if或辅助函数。赛布里夫斯基

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

关于python - 如何在python中使用selenium查找html类中是否存在元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460072/

相关文章:

python - 简单协议(protocol)(如twisted.pb)与消息传递(AMQP/JMS)与Web服务(REST/SOAP)

java - Selenium 网络驱动程序 : How to execute complete Test in testNG for a failed test case

selenium-webdriver - Protractor 的 browser.pause() 暂停但不显示上下文

python selenium点击按钮

selenium - 下载 Firefox 扩展(附加组件)而不安装

ruby - 网络驱动程序。按坐标单击 Canvas 元素

java - 将字符串转换为 WebElement

python - 谷歌实验室 : misleading information about its GPU (only 5% RAM available to some users)

python - 如何从 model.predict() 结果中获取单个值

python - 计算 Skyfield 中两个物体的表观角度分离的更好方法?