首页 > 解决方案 > 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 请帮助我真的迷路了......

标签: pythonseleniumxpath

解决方案


您可以使用 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)

推荐阅读