python - 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)
解决方案
你最后缺少一个额外的空间,试试这个:
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])
这是一个非常简单的示例,并没有考虑很多需要处理以使其稳定的场景,其中一些是:
单击 后,元素可能会消失或重新加载
products_load_button
。为此,我建议您查看 selenium 文档中的显式等待。加载完成后进度条可能会消失/隐藏。
推荐阅读
- scala - 如何在 Scala 中正确使用存在(forSome)用例的类型定义来表达自引用类型
- java - Spring AMQP - 重新创建已删除的队列
- ios - iOS 模拟器没有网络连接
- java - 将我的稳定 python 库从 github 到主 java web 应用程序构建的正确 maven 依赖项
- c# - 从带有按钮、选择字段、响应评估的 WPF/Console C# 应用程序创建交互式 toast 通知
- c++ - 类中函数的内存分配
- python - 尝试使用女服务员解决内部服务器错误 - django - heroku
- powerbi - 在 DAX 中使用 IF CONTAINS 语句的正确方法是什么?
- crystal-reports - Crystal Reports 交叉表汇总数据子集的行数
- python - 如何在 Google Cloud Datalab 中使用 Python 3.8?