2
votes

I've been trying to select the Play button on Spotify's webplayer through selenium for a series of tests, but have been unable to successfully target it. I've tried through CSS Selector:

driver.find_element_by_css_selector("#header .button-primary").click()

and xpath:

driver.find_element_by_xpath("//button[@class=\"button-primary\"]")

This is my error call stack:

Traceback (most recent call last): File "Spotify Dumbass.py", line 37, in test_spotify_dumbass driver.find_element_by_css_selector("#header .button-primary").click() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 396, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 684, in find_element {'using': by, 'value': value})['value'] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 195, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 170, in check_response raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"#header .button-primary"} Stacktrace: at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/[email protected]/components/driver-component.js:10299) at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/[email protected]/components/driver-component.js:603)

2

2 Answers

0
votes

Controlling through Javascript

Is there a JS SDK for the player? Can I send play/pause commands using Javascript?

The Play button doesn’t have any observable events, nor does it listen for any. Playback can only be triggered by the user clicking the button. We’re certainly looking at creating a JS SDK; it’s a bigger project and right now we’re focusing on quickly releasing APIs to open up the general Spotify ecosystem (catalogue, playlists, etc). None of these requests are falling on deaf ears, and we’re working hard to bring out as much functionality as we can.

From Sptotify's Dev Page

2
votes

The "play" button is inside an iframe, switch to it before locating the element:

driver.switch_to.frame("app-player")
play = driver.find_element_by_id("play-pause")
play.click()