如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic [英] How To sign in to Applemusic With Python Using Chrome Driver With Selenium

查看:101
本文介绍了如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有硒的Python编写代码,该代码将登录到Applemusic.

  1. 应打开以下URL:


    参考

    您可以在以下位置找到一些相关的讨论:

    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:

    这篇关于如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆