嗨,我在蘋果應用商店的自動登錄方面被困了一段時間,因為我試圖讓它無頭,而沒有看到執行中的瀏覽器。問題是在使用無頭選項和其他選項執行時,總體上它沒有找到 appleid 的欄位說:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="account_name_text_field"]"}
這是我到目前為止嘗試的代碼:
options = selenium_chrome()
options.add_argument("--enable-javascript")
options.add_argument('headless')
options.add_argument('--window-size=1920x1080')
options.add_argument('--start-maximized')
options.add_argument("--disable-notifications")
options.add_argument('--no-sandbox')
options.add_argument('--verbose')
options.add_experimental_option("prefs", {
"download.default_directory": DOWNLOAD_DIR,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
})
options.add_argument('--disable-gpu')
options.add_argument('--disable-software-rasterizer')
driver = webdriver.Chrome('/path/to/chromedriver', chrome_options=options)
driver.get('https://appstoreconnect.apple.com/login')
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
time.sleep(10)
f = driver.find_element_by_id("aid-auth-widget-iFrame")
driver.switch_to.frame(f)
# driver.implicitly_wait(15)
time.sleep(20)
driver.find_element_by_id('account_name_text_field').send_keys('appleid')
driver.find_element_by_id("sign-in").click()
time.sleep(5)
driver.find_element_by_id('remember-me').click()
time.sleep(3)
driver.find_element_by_id('password_text_field').send_keys('password')
driver.find_element_by_id("sign-in").click()
筆記:
我嘗試獲取 XPATH 和 CSS 選擇器而不是 ID,結果是相同的。
而不是
time.sleep()我嘗試:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='account_name_text_field']"))).send_keys('appleid')
結果是TimeoutException
- 做了一個重試功能,只要找不到該欄位就一直嘗試直到找到它,但這是一種不好的方法,它沒有奏效。
所以我不知道是什么導致了這種行為,在沒有 selenium 選項的情況下找到了該欄位,并且它在視覺上可以很好地登錄,但是每當添加選項時,它就再也找不到輸入文本欄位appleid了......可能是蘋果的東西我不知道。
uj5u.com熱心網友回復:
<input>占位符作為Apple ID的元素位于iframe 中,因此您必須:
誘導WebDriverWait使所需的幀可用并切換到它。
誘導WebDriverWait使所需元素成為可點擊的。
您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
driver.get("https://appstoreconnect.apple.com/login") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#aid-auth-widget-iFrame"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#account_name_text_field"))).send_keys("mastaofthepasta")使用XPATH:
driver.get("https://appstoreconnect.apple.com/login") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='aid-auth-widget-iFrame']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='account_name_text_field']"))).send_keys("mastaofthepasta")
注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC瀏覽器快照:

參考
您可以在以下位置找到一些相關的討論:
- 通過 Selenium 和 python 切換到 iframe
- selenium.common.exceptions.NoSuchElementException:訊息:沒有這樣的元素:嘗試使用 selenium 單擊下一步按鈕時無法找到元素
- python中的硒:NoSuchElementException:訊息:沒有這樣的元素:無法找到元素
更新
當你使用 谷歌瀏覽器無頭并列印page_source控制臺上的輸出是:
<html><head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>Apple</center>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
</body></html>
403 禁止
HTTP403 Forbidden回應狀態碼表示服務器理解請求但拒絕授權。
此狀態類似于401,但對于403 Forbidden狀態碼,重新認證沒有區別。訪問被永久禁止并與應用程式邏輯相關聯,例如對資源的權限不足。
結論
ChromeDriver 啟動的Chrome 瀏覽器被檢測為機器人 并且進一步的導航被阻止。
參考
您可以在以下位置找到一些相關的詳細討論:
- 網站拒絕使用 Selenium 獲取請求
- 在 Selenium 中使用 HttpURLConnection 時如何修復 403 回應,因為鏈接是手動打開的,沒有任何問題
uj5u.com熱心網友回復:
看起來你的問題在這里:
而不是
options.add_argument('headless')
它應該是
options.add_argument('--headless')
即您--在選項引數
UPD
之前缺少
而不是
driver = webdriver.Chrome('/path/to/chromedriver', chrome_options=options)
它可能是
driver = webdriver.Chrome('/path/to/chromedriver', options=options)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420604.html
標籤:
上一篇:我如何在Selenium中列印值,我只得到<generatorobject<genexpr>,而find_elements不起作用
