简体   繁体   中英

How To sign in to Applemusic With Python Using Chrome Driver With Selenium

I am trying to write a code using Python with selenium that will login to Applemusic.

  1. Should open this URL: https://music.apple.com/login
  1. After which the sign in Frame would pop up, I want to be able to input the AppleID (say example@gmail.com) with password say (Apple2020)

  2. I want to be able to open a particular song url or playlist link so that I can manually play the songs.

My code so far:

from time import sleep
import Password
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class Applemusic:
 
def __init__(self, username):
driver = webdriver.Chrome()


    driver.get("https://music.apple.com/login")
    sleep(20)
    for a in username:
        WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "aid-auth-widget-iFrame")))
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "account_name_text_field"))).send_keys(a)
    sleep (10)
    password = "Apple2020"
    for i in password:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "password_text_field"))).send_keys(i)

Applemusic('example@gmail.com')

This is the error:

Traceback (most recent call last): File "/Users/mike/Documents/Python/Applemusic.py", line 21, in Applemusic('example@gmail.com') File "/Users/mike/Documents/Python/Applemusic.py", line 14, in init WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "aid-auth-widget-iFrame"))) File "/Users/mike/Documents/Python/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

I don't know what I'm not doing right. My python knowledge is just average, I would appreciate if anyone can help me achieve my goal.

The Apple ID field is within nested <iframe> elements so you have to:

  • Induce WebDriverWait for the parent frame to be available and switch to it .

  • Induce WebDriverWait for the child frame to be available and switch to it .

  • Induce WebDriverWait for the desired element to be clickable .

  • You can use either of the following Locator Strategies :

    • Using CSS_SELECTOR :

       driver.get('https://music.apple.com/login') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='/includes/commerce/authenticate?product=music']"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title^='Sign In with your Apple']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#account_name_text_field"))).send_keys("Tycoonstory@apple.com")
    • Using XPATH :

       driver.get('https://music.apple.com/login') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, '/includes/commerce/authenticate?product=music')]"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@title, 'Sign In with your Apple')]"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='account_name_text_field']"))).send_keys("Tycoonstory@apple.com")
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

苹果


Reference

You can find a couple of relevant discussions in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM