0

Hay, I try to click the Spotify Play button with Python enter image description here enter image description here

browser.find_element('<button class="btn btn-green false">PLAY</button>').click()

What is the correct code line to click required button?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Malte e
  • 11
  • 5
  • 1
    Please read https://stackoverflow.com/help/how-to-ask to frame your questions better – sureshprasanna70 Sep 24 '19 at 18:33
  • Welcome to SO. Please take the time to read [ask]. It will help you craft solid questions that will hopefully get useful answers. Please don't post screenshots in place of HTML/code. Lastly, this is a violation of Spotify's TOS; you should use their API. – orde Sep 24 '19 at 19:18

1 Answers1

1

You might need to update your code as

browser.find_element('xpath', '//button[.="PLAY"]').click()

Note that you should pass 2 arguments to find_element method:

  • locator strategy, e.g. xpath, css, etc
  • locator value

P.S. Also add browser.maximize_window() to the beginning of script to set browser fullscreen mode (DOM might differs due to different browser window size)

JaSON
  • 4,843
  • 2
  • 8
  • 15
  • they give me in the console this error message: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable – Malte e Sep 24 '19 at 18:38
  • @Maltee , try to `import time` and add `time.sleep(5)` before that line. Does it work now? – JaSON Sep 24 '19 at 18:40
  • @Maltee , check if `print(len(browser.find_elements('xpath', '//button[.="PLAY"]')))` returns `1` – JaSON Sep 24 '19 at 18:45
  • yes that works in a special way? it gives me back the number 3 – Malte e Sep 24 '19 at 18:47
  • @Maltee this means that there are more than 1 PLAY button in DOM. Try more specific locator as `browser.find_element('xpath', '//button[contains(@class, "btn-green") and .= "PLAY"]').click()` – JaSON Sep 24 '19 at 18:52
  • no that dont work too it give me back the same error again: ElementNotInteractableException: Message: element not interactable – Malte e Sep 24 '19 at 18:59
  • oh ok i tryed it with print and this code and it gives me back 2 do you have a idear to make it more specific so it just find 1? – Malte e Sep 24 '19 at 19:01
  • @Maltee how about `browser.find_element('xpath', '//header[@class="TrackListHeader"]//button[contains(@class, "btn-green") and .= "PLAY"]').click()`? Also make sure that you did `browser.maximize_window()` at the very beggining of script – JaSON Sep 24 '19 at 19:04
  • dosnt work but when i put it in print it gives me the number 2 again but this time no error – Malte e Sep 24 '19 at 19:09
  • oh, i missed that i make the screen full size now it worke thank you very much you saved my live <3 – Malte e Sep 24 '19 at 19:12
  • @Maltee , glad to hear) You can accept the answer now if it solved your problem. Thanks – JaSON Sep 24 '19 at 19:15
  • year i did thank you for your time and your quick answears :) – Malte e Sep 24 '19 at 19:19
  • What class is the `browser` from? Or what in particular is `browser`? Looking for a similar solution but don't know where `browser` comes from. – developer01 Apr 21 '22 at 21:32
  • @developer01 `browser` is an instance of `WebDriver` – JaSON Apr 22 '22 at 08:05