Selenium find_elements_by_class_name不返回完整列表

时间:2020-04-06 18:37:04

标签: python python-3.x selenium selenium-webdriver

我正在尝试从使用Selenium和Python 3的网站上抓取一些数据,但遇到了一些问题。

我正试图从中获取信息的网站代码:

<uni-view data-v-13033eed="" class="select-container" style="height: 1777px;">
    <uni-view data-v-13033eed="" class="size-list-wrap">
        <uni-view data-v-13033eed="" data-section="0" data-row="0" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">36</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3789</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="1" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">37.5</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3899</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="2" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">38.5</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3839</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="3" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">40</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3479</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="4" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">41</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3629</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="5" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">42.5</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥3869</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="6" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">44</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥4099</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="7" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">45</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥4409</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="8" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">46</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥4209</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="9" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">47.5</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥5079</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="10" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">48</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥--</uni-view>
        </uni-view>
        <uni-view data-v-13033eed="" data-section="0" data-row="11" data-item="[object Object]" class="select-size-info  cover-info">
            <uni-view data-v-13033eed="" class="size">48.5</uni-view>
            <uni-view data-v-13033eed="" class="size-price">¥--</uni-view>
        </uni-view>
    </uni-view>
</uni-view>

我想获取“ size-price”类并打印找到的每个元素。我的代码如下:

find_price = driver.find_elements_by_class_name('size-price')
for item in find_price:
print(item.text)

但是,仅返回12个元素中的4个。

这是我在控制台中看到的:

¥4209
¥5079
¥--
¥--

我想念什么吗?

1 个答案:

答案 0 :(得分:0)

有时候网页需要花费一些时间来完善网页的加载,因此可能需要等待一段时间才能解决问题。

使用此方法.presence_of_all_elements_located

wait = WebDriverWait(driver, 60)
find_price = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'size-price')))

for item in find_price:
    print(item.text)

正在导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC