python - Python找不到spotify滚动框的xpath
问题描述
所以基本上我正在学习 python,并认为制作一个进入 Spotify 并返回你喜欢但不在播放列表中的歌曲的机器人会很有趣。Spotify 在播放器中有一些奇怪的功能,它不允许您检查特定元素,因此我必须通过检查器搜索以找到喜欢歌曲中的滚动框。我正在尝试保存到 scroll_box 的路径,但它只是发回:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".scroller.context-event"}
(Session info: chrome=81.0.4044.113)
我已经尝试了很多 find_element_by 的变体和很多路径,但我无法得到它。最后,我需要向下滚动框并加载所有歌曲,然后从那里开始工作。这是我的代码:
from selenium import webdriver
from time import sleep
from secrets import pw2
from secrets import email
import selenium
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
class Spotbot:
def __init__(self, email, username, pw2):
self.driver = webdriver.Chrome()
self.driver.get('https://spotify.com')
self.driver.maximize_window()
sleep(1)
self.driver.find_element_by_xpath("//a[contains(text(), 'Log In')]")\
.click()
sleep(1)
self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
.send_keys(email)
self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
.send_keys(pw2)
self.driver.find_element_by_xpath("//button[contains(text(), 'Log In')]")\
.click()
sleep(2)
self.driver.get('https://open.spotify.com/collection/tracks')
sleep(2)
#scroll_box = self.driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[4]/div[1]/div/div[2]/div[1]/section/div/div/div[2]/section/ol')
page = self.driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[4]/div[1]/div/div[2]/div[1]/section/div/div/div[2]')
self.driver.execute_script("""arguments[0].scrollTo(0, arguments[0].scrollHeight);""", page)
sleep(10)
Spotbot(email, 'kallen_selby', pw2)
这是滚动框的链接:https : //open.spotify.com/collection/tracks 请帮助我真的迷路了......
解决方案
您可以使用 javascript 滚动,但不确定“滚动框”是什么意思:
#Specific height (1080 pixel in my monitor)
driver.execute_script("window.scrollTo(0, 1080);")
#Bottom of page
page = driver.find_element_by_xpath('/html')
driver.execute_script("""arguments[0].scrollTo(0, arguments[0].scrollHeight);""", page)
推荐阅读
- maven - 通过“构建工具”在多模块中的 Maven 站点描述符
- excel - 名称管理器中的公式显示 1 个短
- symfony - Symfony 3.4 项目。如何添加搜索功能?
- c# - 读取端数据库最终与事件处理程序保持一致。如何分段读取端更新
- d3.js - 对使用相同路径数据、d3.js 中的不同投影的多个地图使用本地 geoPath 变量
- laravel - Laravel:很多错误
- ios - 在 IOS 上使用 AWS AppSync 将图像上传到 S3
- excel - 在 Excel 单元格中包装逗号分隔的数字
- android - 如何从资产文件夹中打开pdf文件
- javascript - 如何更新 Tensorflow.js 模型的权重?