【问题标题】:Python Code to add songs to a Spotify playlist using Selenium使用 Selenium 将歌曲添加到 Spotify 播放列表的 Python 代码
【发布时间】:2019-11-16 19:08:38
【问题描述】:

登录后,我的程序开始循环播放歌曲列表,以将它们添加到我的 Spotify 播放列表中。但是在第一个循环之后,它引发了“过时的元素引用:元素未附加到页面文档”异常。 Link I'm working on

driver=webdriver.Chrome()
driver.maximize_window()
actionChain = ActionChains(driver)
driver.get('https://open.spotify.com/browse/featured')
#Login Procedure
psw=''
login=driver.find_element_by_xpath("//button[2]").click()
sleep(1)
email=driver.find_element_by_id('login-username').send_keys('abc@yahoo.com')
password=driver.find_element_by_id('login-password').send_keys(psw)
login=driver.find_element_by_id('login-button').click() 

ignored_exceptions=(StaleElementReferenceException,NoSuchElementException)

def wdwfind(path):
    return WebDriverWait(driver, 15,ignored_exceptions=ignored_exceptions).until(
            EC.presence_of_element_located((By.XPATH,(path))))

def wdwclick(path):
    return WebDriverWait(driver, 15,ignored_exceptions=ignored_exceptions).until(
            EC.element_to_be_clickable((By.XPATH,(path))))


for n in range(len(songs)):
    wdwfind("//li[2]/div/a/div/span").click() #going to search tab
    wdwfind("//input").send_keys(songs[n]) #sending elements to navigation bar
    gotosong=wdwclick("//a[@class='d9eb38f5d59f5fabd8ed07639aa3ab77-scss _59c935afb8f0130a69a7b07e50bac04b-scss']") #right clicking the name of the song
    actionChain.context_click(gotosong).perform()
    wdwfind("//nav/div[4]").click() #Selecting the add to playlist option
    wdwfind("//div[@class='mo-coverArt-hoverContainer']").click() #clicking on the playlist to add the song to
    sleep(2) 
    clear=wdwclick("//input[@class='_2f8ed265fb69fb70c0c9afef329ae0b6-scss']").send_keys(Keys.SHIFT,Keys.ARROW_UP) #clearing the search box
    driver.refresh()
    sleep(1)

【问题讨论】:

  • 检查你的 DOM 没有改变,因为当 DOM 改变并且当你试图对其执行任何操作时元素不可用时会发生“stale element reference:”异常
  • 那么你能帮我解决这个问题吗?
  • 哪行代码抛出异常?
  • 另外,songs[] 中有什么内容?这是一个字符串列表,还是别的什么?
  • songs[] 是一个字符串列表。它包含我想添加的歌曲。

标签: python-3.x selenium selenium-webdriver spotify


【解决方案1】:

我已调查您的问题背后的原因。如果您尝试在播放列表中添加歌曲,请找到解决方案。由于 DOM 中的动态元素,您面临上述问题。同样在运行以下代码后,我得到了会员窗口,因此无法继续。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains

# Open Chrome
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get("https://open.spotify.com/browse/featured")





element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//li[2]/div/a/div/span")))
element.click()
WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.XPATH,"//input[@class='SearchInputBox__input']"))).send_keys("songs")


driver.find_element_by_xpath("//*[text()='Log in']").click()

WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.XPATH,"//input[@id='login-username']"))).send_keys("")

WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.XPATH,"//input[@id='login-password']"))).send_keys("")

WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//button[@id='login-button']"))).click()

items = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='react-contextmenu-wrapper']/div/div/a")))
print(len(items))


for song in items:
    print song.text
    actionChains = ActionChains(driver)

    actionChains.context_click(song).perform()
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//*[text()='Add to Playlist']")))
    element.click()

    element12 = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//button[@class='btn asideButton-button btn-green btn-small']")))
    actionChains.move_to_element(element12).click().perform()
    actionChains.context_click(song).perform()
    element00=WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, "//input[@class='inputBox-input']"))).send_keys("testPlayList")

    element11 = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//div[@class='button-group button-group--horizontal']//div[2]/button")))
    actionChains.move_to_element(element11).click().perform()

    elem=WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, "//div[@class='TrackListHeader__entity-name']//span")))
    print elem.text
    break

【讨论】:

  • 我已经编辑了代码,这将帮助您从会员窗口继续前进。
  • 我会接受您的回答,但您能否先帮我将歌曲添加到播放列表。 songs=['I Want It that way','Believer']。我希望将这些歌曲添加到播放列表中,但第一次运行后 DOM 会刷新。如果您能帮助我阻止 DOM 更改或其他替代方法,我将不胜感激。
  • 亲爱的 Dipak, 你似乎误解了我的意思。您的代码正在添加在搜索一首特定歌曲期间出现的所有歌曲建议。但我打算在搜索查询中一首一首发送100首歌曲,并将每次搜索的第一个结果添加到播放列表中。如果可能,请通过 mehta_priyesh7401@yahoo.com 与我联系,以便我更好地解释我的目标。
  • 我的代码终于可以工作了。我从你的代码中学到了很多新东西。非常感谢!
  • 对不起@PriyeshMehta 我正面临stackoverflow.com/questions/58914348/… 问题并努力解决它,所以没有机会修改上面的代码,但很高兴听到你解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 2012-12-20
  • 2017-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多