如何通过 Selenium 和 Python 在我的页面上向下滚动?

How to scroll down on my page through Selenium and Python?

正在尝试向下滚动到页面底部 https://silpo.ua/offers/?categoryId=13 但没有结果(没有动静)

我的代码:

import bs4
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

URL = "https://silpo.ua/offers/?categoryId=13"
driver = webdriver.Firefox()
driver.get(URL)

page = driver.find_element_by_tag_name("html")
page.send_keys(Keys.PAGE_DOWN)

html = driver.page_source

您可以使用 actions.move_to_element

移动到底部的 copyright class 元素
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

url ="https://silpo.ua/offers/?categoryId=13"
driver = webdriver.Chrome()
driver.get(url)
element = driver.find_element_by_css_selector(".copyrights")
actions = ActionChains(driver)
actions.move_to_element(element).perform()

您可以对此进行更改,例如,假设您想转到上一个产品:

element = driver.find_elements_by_css_selector(".product-list__item-content")[-1]
actions = ActionChains(driver)
actions.move_to_element(element).perform()

向下滚动到页面底部的方法有多种。根据 url https://silpo.ua/offers/?categoryId=13版权 消息位于页面底部。因此,您可以使用 scrollIntoView() 方法在 Viewport 中滚动 copyright 消息,如下所示:

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

URL = "https://silpo.ua/offers/?categoryId=13"
driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get(URL)
copyright = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.copyrights")))
driver.execute_script("return arguments[0].scrollIntoView(true);", copyright)