如何使用 Selenium 和 Python 将 AppleID 发送到登录字段

How to send AppleID to the Sign in field using Selenium and Python

为什么不正确:

网页:https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=a01459d797984726ee0914a7097e53fad42b70e1f08d09294d14523a1d4f61e1&rv=2&path=

遵循的步骤:

  1. 检查,元素选择器,点击Apple ID字段框

  2. 显示:

<input type="text" class="force-ltr form-textbox form-textbox-text" id="account_name_text_field" can-field="accountName" autocomplete="off" autocorrect="off" autocapitalize="off" aria-required="true" required="required" aria-describedby="apple_id_field_label" spellcheck="false" ($focus)="appleIdFocusHandler()" ($keyup)="appleIdKeyupHandler()" ($blur)="appleIdBlurHandler()" placeholder="Apple&nbsp;ID" autofocus="">
  1. 我的代码:
driver.find_element_by_id("account_name_text_field").send_keys(username)
  1. 错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="account_name_text_field"]"}

我什至休眠了 100 秒,以防它因为加载需要一段时间而出错。

拍拍自己的背,因为有很多积极的收获,因为您已经确定了完美的所需元素。但是,所需元素位于 <iframe> 中,因此要在所需元素上调用 click(),您必须:

  • 诱导 WebDriverWait 以获得所需的 框架并切换到它.
  • 诱导 WebDriverWait 使所需的 元素可点击
  • 您可以使用以下解决方案:

    • 代码块:

      driver.get("https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=a01459d797984726ee0914a7097e53fad42b70e1f08d09294d14523a1d4f61e1&rv=2&path")
      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("Tom")
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
  • 浏览器快照:


参考

在这里您可以找到几个相关的讨论: