【问题标题】:Python can't find xpath for spotify scroll boxPython找不到spotify滚动框的xpath
【发布时间】: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

标签: python selenium xpath


【解决方案1】:

好的,我找到了解决我自己问题的方法:)

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()
        self.username = username
        self.email = email
        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)

    def get_liked_songs(self):
        self.driver.get('https://open.spotify.com/collection/tracks')
        sleep(2)
        #(/html/body/div[3]/div/div[3]/div[4]/div[1]/div/div[2])
        scroll_box = self.driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[4]/div[1]/div')
        last_ht, ht = 0, 1
        while last_ht != ht:
            last_ht = ht
            sleep(1)
            ht = self.driver.execute_script("""
                arguments[0].scrollTo(0, arguments[0].scrollHeight); 
                return arguments[0].scrollHeight;
                """, scroll_box)
        sleep(1)
        liked_songs= scroll_box.find_elements_by_tag_name('a')
        songs= [name.text for name in liked_songs if name != '']
        print(songs)






my_bot = Spotbot(email, 'kallen_selby', pw2)
my_bot.get_liked_songs()

所以我只需要备份 xpath,然后使用 while 循环滚动,直到我不能再滚动。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,我必须弄清楚如何滚动,以便帮助坚果我如何告诉它滚动包含歌曲而不是页面的特定“框”?
    • 当我运行这个脚本时它也不会向下滚动,因为实际页面不能只向下滚动页面中的框。我需要做的是你写 /html 的部分,我需要它走得更远,但每当我这样做时,它都会给出错误消息
    • 我将其更新为 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]/section/ol') self.driver.execute_script("""arguments[0].scrollTo(0, arguments[0].scrollHeight );""", page) sleep(10) 但是什么也没发生,但我没有错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2018-06-14
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多