Python 如何使用selenium缓慢滚动到页面末尾,以便动态加载内容
在一个个人项目中,我在am上工作,我需要使用selenium从动态站点中刮取项目名称 要获取所有数据,您需要滚动到底部 然而,如果你滚动到底部,很快你只会在底部得到项目的名称,这会变得更加棘手。无论您等待多长时间,您仍然可以获得范围中的项目 所以我想我可以慢慢地滚动到底部,但它似乎不起作用 下面是我的演示代码来说明这个问题 有什么想法吗 额外的导入和配置 解决方案 它在这种情况下是如何应用的Python 如何使用selenium缓慢滚动到页面末尾,以便动态加载内容,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,在一个个人项目中,我在am上工作,我需要使用selenium从动态站点中刮取项目名称 要获取所有数据,您需要滚动到底部 然而,如果你滚动到底部,很快你只会在底部得到项目的名称,这会变得更加棘手。无论您等待多长时间,您仍然可以获得范围中的项目 所以我想我可以慢慢地滚动到底部,但它似乎不起作用 下面是我的演示代码来说明这个问题 有什么想法吗 额外的导入和配置 解决方案 它在这种情况下是如何应用的 这根本不会滚动到结尾不,我不会关闭通知,这需要一些时间…请将代码编辑为完整的代码片段。我在尝试取消注释h
这根本不会滚动到结尾不,我不会关闭通知,这需要一些时间…请将代码编辑为完整的代码片段。我在尝试取消注释headless选项时也会这样做,我在关闭通知后会看到它工作了…但只有在页面完全加载之前关闭它,通知才是阻止一切的通知最终代码可以自动关闭通知你很好,再见
url='https://shopzetu.com/search?type=product,article,page&q=dress'
driver.get(url)
#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # this works but you get items at bottom only
#this scrolls slowly to end
driver.execute_script("function pageScroll() {window.scrollBy(0,50);scrolldelay = setTimeout('pageScroll()',1000);}pageScroll()")
time.sleep(2)
products =driver.find_elements_by_class_name("grid-product__content")
for product in products:
name=product.find_element_by_class_name("grid-product__title").text
print(name)
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=chrome_options)
page = driver.find_element_by_tag_name("html")
page.send_keys(Keys.END)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=chrome_options)
url = 'https://shopzetu.com/search?type=product,article,page&q=dress'
driver.get(url)
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//button[text()='No thanks']"))).click()
page = driver.find_element_by_tag_name("html")
page.send_keys(Keys.END)
products = driver.find_elements_by_class_name("grid-product__content")
for product in products:
name = product.find_element_by_class_name("grid-product__title").text
print(name)
page.send_keys(Keys.END)