簡體   English   中英

如何使用 Selenium 和 Python 在 https://www.shopdisney.com/ 中找到創建帳戶元素

[英]How to locate the Create an Account element within https://www.shopdisney.com/ using Selenium and Python

我一直在嘗試使用 selenium 和 python 單擊此網頁上的“創建帳戶”按鈕,但 python 似乎找不到該元素。 這是我當前的代碼:

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen")
time.sleep(12)
accountcreate = driver.find_element_by_class_name ('btn-group btn-group-create-account ng-scope')
accountcreate.click()

每次我運行它時,chrome都會打開網頁,但它沒有點擊按鈕,我得到這個響應:

  File "skit.py", line 8, in <module>
    link = driver.find_element_by_class_name ('btn-group btn-group-create-account ng-scope')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn-group btn-group-create-account ng-scope"}
  (Session info: chrome=83.0.4103.97)

我嘗試使用不同的方法來識別元素,例如 XPath、css 等,但我仍然無法找到並單擊它。 我相信它與 iframes 有關,但我不完全確定。 有誰知道如何解決這個問題?

謝謝!

文本為Create an Account的鏈接位於<iframe>中,因此您必須:

  • 誘導WebDriverWait使所需的幀可用並切換到它

  • 為所需的element_to_be_clickable()誘導WebDriverWait

  • 您可以使用以下定位器策略

  • LINK_TEXT

     driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen") WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"disneyid-iframe"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Create an Account"))).click()
  • 使用CSS_SELECTOR

     driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen") WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='disneyid-iframe']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-secondary.ng-isolate-scope"))).click()
  • 使用XPATH

     driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen") WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='disneyid-iframe']"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Create an Account']"))).click()
  • 注意:您必須添加以下導入:

     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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM