【发布时间】: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