首页 > 解决方案 > 使用 Selenium 将歌曲添加到 Spotify 播放列表的 Python 代码

问题描述

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

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)

标签: python-3.xseleniumselenium-webdriverspotify

解决方案


我已经调查了您问题背后的原因。如果您尝试在播放列表中添加歌曲,请找到解决方案。由于 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

推荐阅读