![](/img/trans.png)
[英]How do I click this sign in button using python selenium chrome driver:
[英]How To sign in to Applemusic With Python Using Chrome Driver With Selenium
我正在嘗試使用 Python 和 selenium 編寫將登錄 Applemusic 的代碼。
之后會彈出登錄框架,我希望能夠輸入 AppleID(例如 example@gmail.com)和密碼(Apple2020)
我希望能夠打開特定歌曲 url 或播放列表鏈接,以便我可以手動播放歌曲。
到目前為止我的代碼:
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')
這是錯誤:
Traceback(最近一次通話最后):文件“/Users/mike/Documents/Python/Applemusic.py”,第 21 行,Applemusic('example@gmail.com')文件“/Users/mike/Documents/Python/Applemusic .py",第 14 行,在init WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "aid-auth-widget-iFrame"))) 文件 "/Users/mike/Documents/Python/ venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py”,第 80 行,直到引發 TimeoutException(消息,屏幕,堆棧跟蹤)selenium.common.exceptions.TimeoutException:消息:
我不知道我做錯了什么。 我的 python 知識只是平均水平,如果有人能幫助我實現我的目標,我將不勝感激。
Apple ID字段位於嵌套的<iframe>
元素中,因此您必須:
誘導WebDriverWait使父框架可用並切換到它。
誘導WebDriverWait使子框架可用並切換到它。
誘導WebDriverWait使所需元素成為可點擊的。
您可以使用以下任一定位器策略:
使用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")
使用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")
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
瀏覽器快照:
您可以在以下位置找到一些相關的討論:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.