如何查找div中是否有元素?
How to find if there is an element in a div?
我有一个帖子列表,其中一些有反应,一些没有。我想提取每个帖子的反应数量,问题是那些没有的人,没有 HTML 代码。
我尝试使用 print 进行调试。
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")))
-----
Output: 4 - This is good, because I have 4 divs
print(len(driver.find_elements_by_xpath("//span[@class='v-align-middle social-details-social-counts__reactions-count']"))
----
Output:2 - This is good, because I have 2 posts that got reactions
当我试图查看某个帖子是否有反应时,
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath("//span[@class='v-align-middle social-details-social-counts__reactions-count']")))
----
Output: 2 - Why? Because the second div is empty...
HTML 代码
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
我发现了问题。在更好地记录自己之后,我发现使用子元素 .//
而不是 //
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath(".//span[@class='v-align-middle social-details-social-counts__reactions-count']")))
要定位延续元素,请使用 .
尝试以下:
driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath(".//descendant::span[@class='v-align-middle social-details-social-counts__reactions-count']")
我有一个帖子列表,其中一些有反应,一些没有。我想提取每个帖子的反应数量,问题是那些没有的人,没有 HTML 代码。
我尝试使用 print 进行调试。
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")))
-----
Output: 4 - This is good, because I have 4 divs
print(len(driver.find_elements_by_xpath("//span[@class='v-align-middle social-details-social-counts__reactions-count']"))
----
Output:2 - This is good, because I have 2 posts that got reactions
当我试图查看某个帖子是否有反应时,
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath("//span[@class='v-align-middle social-details-social-counts__reactions-count']")))
----
Output: 2 - Why? Because the second div is empty...
HTML 代码
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
我发现了问题。在更好地记录自己之后,我发现使用子元素 .//
而不是 //
print(len(driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath(".//span[@class='v-align-middle social-details-social-counts__reactions-count']")))
要定位延续元素,请使用 .
尝试以下:
driver.find_elements_by_xpath("//div[@class='occludable-update ember-view']")[1].find_elements_by_xpath(".//descendant::span[@class='v-align-middle social-details-social-counts__reactions-count']")