【发布时间】:2020-04-23 16:06:29
【问题描述】:
所以基本上我正在学习 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 请帮助我真的迷路了......
【问题讨论】:
-
您要处理哪个元素?您能否提供更多详细信息,因为 xpath 不会帮助您在网站上找到您的元素
-
我正在尝试向下滚动歌曲列表。所以我不想向下滚动页面,而是页面内的歌曲列表。 open.spotify.com/collection/tracks