After logging in, my program starts looping over a list of songs to add them to my Spotify Playlist. But after the first loop, it raises the "stale element reference: element is not attached to the page document" exception. 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)
I have investigated reason behind your issue. please find solution if you are trying to add songs in playlist. You are facing above issue due to the dynamic elements in the DOM. Also after running below code I am getting membership window so cant proceed further.
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