首页 > 解决方案 > selenium python产品加载按钮不起作用

问题描述

页面共有 790 个产品,我编写了 selenium 代码以自动单击产品加载按钮,直到它完成加载所有 790 个产品。不幸的是,我的代码无法正常工作并出现错误。这是我的完整代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time


driver =  webdriver.Chrome()
driver.maximize_window()
url ='https://www.billigvvs.dk/maerker/grohe/produkter?min_price=1'
driver.get(url)

time.sleep(5)

#accept cookies 
try:
   driver.find_element_by_xpath("//button[@class='coi-banner__accept']").click()
except:
    pass
    print('cookies not accepted')

# Wait 20 seconds for page to load.
timeout = 20
try:
    WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='productbox__info__name']")))
except TimeoutException:
    print("Timed out waiting for page to load")
    browser.quit()


#my page load button not working. I want to load all 790 product in this page 
products_load_button = driver.find_element_by_xpath("//div[@class='filterlist__button']").click()

我得到的错误:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='filterlist__button']"}
  (Session info: chrome=87.0.4280.88)

错误消息说无法找到元素,但看到图片说我正在选择正确的元素。在此处输入图像描述

标签: pythonselenium

解决方案


你最后缺少一个额外的空间,试试这个:

products_load_button = driver.find_element_by_xpath("//div[@class='filterlist__button ']").click()

当您使用选择器时,直接从页面复制和粘贴始终是一个好习惯,这将在未来省去很多麻烦。

编辑:

检查是否所有元素都已加载的 while 循环类似于以下内容:

progress_bar_text = driver.find_element_by_css("div.filterlist__pagination__text").text

# From here you could extract the total items and the loaded items
# Note: I am doing this because I don't have access to the page, probably
# there is a better way to found out if the items are loaded taking
# taking a look into the attributes of the progressBar

total_items = int(progress_bar_text.split()[4])
loaded_items = int(progress_bar_text.split()[1])

while loaded_items < total_items:
    # Click the product load button until the products are loaded
    product_load_button.click()

    # Get the progress bar text and updates the loaded_items count
    progress_bar_text = driver.find_element_by_css("div.filterlist__pagination__text").text
    loaded_items = int(progress_bar_text.split()[1])

这是一个非常简单的示例,并没有考虑很多需要处理以使其稳定的场景,其中一些是:

  1. 单击 后,元素可能会消失或重新加载products_load_button。为此,我建议您查看 selenium 文档中的显式等待。

  2. 加载完成后进度条可能会消失/隐藏。


推荐阅读