python - 试图滚动到弹出页面窗口的底部
问题描述
我正在尝试滚动到 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()
解决方案
您可以获取带有 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()
推荐阅读
- highcharts - Highchart 气泡图阴影
- javascript - 不要在单击浏览器的后退按钮时重新加载组件 - Angular
- r - 添加一列后DataFrame nrows null
- tortoisegit - 如何使用 TortoiseGit 比较两个版本中的两个文件?
- node.js - Node.js 需要 req.path?
- angular - Angular:如何设置具有多个条件的 [占位符]
- node.js - 阅读来自超级管理员帐户的委派电子邮件时,Gmail API“电子邮件的委派被拒绝”错误
- datadog - 如何在没有 Datadog 代理的情况下将日志发送到 Datadog
- python - python / Jupyter Notebook中的无限循环停止工作
- selenium - 机器人框架:如果任何关键字在循环内失败,则继续 FOR 循环