Learn selenium Automation with Spotify | Part 3 | Python | AI Assistant


Question from the previous Part 

To click() on the SVG  icon you can use the following solution:
  • driver.find_element_by_xpath('//div[@class="some-class"]/*[name()="svg"][@aria-label="Search"]').click()

Code : 
  1. import os
  2. from time import sleep
  3. import config

  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver import ActionChains
  7. from selenium.webdriver.support.wait import WebDriverWait
  8. from selenium.webdriver.support import expected_conditions as EC

  9. options = webdriver.ChromeOptions()
  10. options.headless = True # if false all the stuff will load in backend
  11. # Driver path cheak your current browser first than download respectively >>
  12. # chrome://settings/help

  13. url = 'https://accounts.spotify.com/en/login?continue=https%3A%2F%2Fopen.spotify.com%2F'


  14. def login(Email, Password):

  15. global driver,wait
  16. driver = webdriver.Chrome('C:\PythonX\Python_Exp_yt\Spotify Automation\chromedriver.exe')
  17. wait = WebDriverWait(driver, 10)
  18. driver.maximize_window()
  19. driver.get(url)

  20. driver.find_element(By.CSS_SELECTOR, "#login-username").send_keys(Email)
  21. driver.find_element(By.CSS_SELECTOR, "#login-password").send_keys(Password)

  22. wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='login-button'] div:nth-child(1)"))).click()


  23. # spotdl https://open.spotify.com/track/0VjIjW4GlUZAMYd2vXMi3b

  24. def download():
  25. str1 = "spotdl "
  26. str2 = driver.current_url
  27. str = str1 + str2
  28. os.system(str)
  29. print("downloading")

  30. def like_song():
  31. driver.find_element(By.XPATH,'//*[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/button[1]').click()
  32. print("liked the song")

  33. def playlist():
  34. driver.find_element(By.XPATH,'//*[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/button[2]').click()

  35. # Hover to playlist
  36. sub_menu = driver.find_element(By.XPATH, "//span[normalize-space()='Add to playlist']")
  37. chain = ActionChains(driver)
  38. chain.move_to_element(sub_menu).perform()

  39. # Add song in anime_song playlist

  40. wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@role='menuitem']//span[@dir='auto'][normalize-space()='anime song']"))).click()


 An update will be soon added :)

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post