如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic [英] How To sign in to Applemusic With Python Using Chrome Driver With Selenium
问题描述
我正在尝试使用带有硒的Python编写代码,该代码将登录到Applemusic.
- 应打开以下URL:
参考
您可以在以下位置找到一些相关的讨论:
- 在iframe下处理#document的方式
- 通过Selenium和python切换到iframe
- selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试单击带有硒的下一步按钮时找不到元素
- Python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素
I am trying to write a code using Python with selenium that will login to Applemusic.
- Should open this URL: https://music.apple.com/login
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)
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:
- Ways to deal with #document under iframe
- Switch to an iframe through Selenium and python
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
这篇关于如何将Chrome驱动程序与Selenium一起使用Python登录到Applemusic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!