首页 > 解决方案 > 试图滚动到弹出页面窗口的底部

问题描述

我正在尝试滚动到 Stockx 弹出页面的底部,以收集一件商品的所有近期销售价格。但我似乎无法到达弹出窗口的底部以单击加载更多按钮。这就是我到目前为止所拥有的。请帮忙。

import bs4 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys 

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
driver.get("https://stockx.com/supreme-patchwork-mohair-cardigan-multicolor")
time.sleep(3)
driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/div[2]/div[9]/div/div/div/div[2]/div/div[2]/div/div/button').click()
driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/div[2]/div[9]/div/div/div/div[2]/div/div[1]/div[2]/a').click()
time.sleep(3)
element_in_popup = driver.find_element_by_xpath('/html/body/div[9]/div/div/div/div[2]/div')
while EC.presence_of_element_located((By.XPATH,'/html/body/div[9]/div/div/div/div[2]/div/button')):
    element_in_popup.send_keys(Keys.END) # Use send_keys(Keys.HOME) to scroll up to the top of page
    driver.find_element_by_xpath('/html/body/div[9]/div/div/div/div[2]/div/button').click()
    

标签: pythonseleniumweb-scrapingpopupwindow

解决方案


您可以获取带有 xpath 的按钮,然后尽可能多地单击它。这是我用来执行此操作的代码:

    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("https://stockx.com/supreme-patchwork-mohair-cardigan-multicolor")
    time.sleep(2)

    confirm_location_btn = driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/section/div/div[2]/button')
    confirm_location_btn.click()
    time.sleep(2)

    okay_cookies_btn = driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/button')
    okay_cookies_btn.click()
    time.sleep(2)

    view_all_sales_btn = driver.find_element_by_xpath('//*[@id="Supreme Patchwork Mohair Cardigan Multicolor"]/div[2]/div[3]/a')
    view_all_sales_btn.click()
    time.sleep(2)

    thanks_btn = driver.find_element_by_xpath('/html/body/div[7]/div/div/div/div[2]/div/div[1]/div/button')
    thanks_btn.click()
    time.sleep(2)

    load_more_btn = driver.find_element_by_xpath('/html/body/div[7]/div/div/div/div[2]/div/button')
    load_more_btn.click()
    time.sleep(2)
    load_more_btn.click()

推荐阅读