如何使用 python 中的硒查找 html 的 class 中是否存在元素
How to find whether an element is present or not in a class of html using selenium in python
我必须相同的划分类型:
1) 其中包含特定 class (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) 没有那个 class (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>
有什么方法可以检查 class (tile-freeCancellation) 的元素是否存在于主要元素 (class tile-duration) 中?
我正在为这个程序使用 python3 并使用函数 find_element_by_class_name 来查找 main class
的元素
selenium returns 的元素对象有自己的 find_element_by_* 方法,将搜索限制在它们的后代中,因此您可以这样做:
# Get a reference to both of the main elements
elements = driver.find_elements_by_class_name('tile-duration')
# helper function for checking presence of inner element
def free_cancel_descendants(element):
return element.find_elements_by_class_name('tile-freeCancellation')
# use it, e.g. to filter the divs:
free_main_divs = [el for el in elements if free_cancel_descendants(el)]
我的尝试-三元运算(if) or if or helper function as by @W. Cybriwsky
if len(driver.find_elements_by_class_name("tile-freeCancellation"))>0:
#do something
else:
# do another
如果是嵌套元素,例如select a div(motherelement) 然后在里面搜索。
if len(motherelement.find_elements_by_class_name("tile-freeCancellation"))>0:
#do something
else:
# do another
我必须相同的划分类型:
1) 其中包含特定 class (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) 没有那个 class (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>
有什么方法可以检查 class (tile-freeCancellation) 的元素是否存在于主要元素 (class tile-duration) 中?
我正在为这个程序使用 python3 并使用函数 find_element_by_class_name 来查找 main class
的元素selenium returns 的元素对象有自己的 find_element_by_* 方法,将搜索限制在它们的后代中,因此您可以这样做:
# Get a reference to both of the main elements
elements = driver.find_elements_by_class_name('tile-duration')
# helper function for checking presence of inner element
def free_cancel_descendants(element):
return element.find_elements_by_class_name('tile-freeCancellation')
# use it, e.g. to filter the divs:
free_main_divs = [el for el in elements if free_cancel_descendants(el)]
我的尝试-三元运算(if) or if or helper function as by @W. Cybriwsky
if len(driver.find_elements_by_class_name("tile-freeCancellation"))>0:
#do something
else:
# do another
如果是嵌套元素,例如select a div(motherelement) 然后在里面搜索。
if len(motherelement.find_elements_by_class_name("tile-freeCancellation"))>0:
#do something
else:
# do another