我必须有相同的划分类型:
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/