【问题标题】:How to scroll down on my page through Selenium and Python?如何通过 Selenium 和 Python 在我的页面上向下滚动?
【发布时间】:2019-05-03 10:11:43
【问题描述】:

试图向下滚动到页面底部 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

【问题讨论】:

标签: python selenium selenium-webdriver scroll webdriverwait


【解决方案1】:

有几种方法可以向下滚动到页面底部。根据网址https://silpo.ua/offers/?categoryId=13copyright 消息位于页面底部。因此,您可以使用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)

【讨论】:

  • 谢谢您,先生!如何使用选择器“product-list__item-image”始终跳转到所有元素?我可以跳到第一个,但不能跳到下一个。
【解决方案2】:

您可以使用actions.move_to_element 移动到底部的copyright 类元素

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多