简体   繁体   English

如何使用 Selenium 和 Python 在 https://www.shopdisney.com/ 中找到创建帐户元素

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

I have been trying to click the "Create an Account" button on this webpage with selenium and python but python cannot seem to find the element.我一直在尝试使用 selenium 和 python 单击此网页上的“创建帐户”按钮,但 python 似乎找不到该元素。 Here is my current code:这是我当前的代码:

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()

Every time I run it, chrome opens to the webpage but it does not click on the button and I get this response:每次我运行它时,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)

I have tried to use different methods to identify the element such as XPath, css, and more but I still cannot manage to locate it and click it.我尝试使用不同的方法来识别元素,例如 XPath、css 等,但我仍然无法找到并单击它。 I believe it has something to do with Iframes but I am not completely sure.我相信它与 iframes 有关,但我不完全确定。 Does anyone have any idea on how to solve this?有谁知道如何解决这个问题?

Thanks!谢谢!

The link with text as Create an Account is within an <iframe> so you have to:文本为Create an Account的链接位于<iframe>中,因此您必须:

  • Induce WebDriverWait for the desired frame to be available and switch to it .诱导WebDriverWait使所需的帧可用并切换到它

  • Induce WebDriverWait for the desired element_to_be_clickable() .为所需的element_to_be_clickable()诱导WebDriverWait

  • You can use the following Locator Strategies :您可以使用以下定位器策略

  • LINK_TEXT : 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()
  • Using CSS_SELECTOR :使用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()
  • Using XPATH :使用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()
  • 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:浏览器快照:

创建一个帐户

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python - How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python 如何使用 Selenium 和 Python 在 https://mail.protonmail.com/ 注册页面中单击创建帐户按钮 - How to click on the Create Account button within https://mail.protonmail.com/ signup page using Selenium and Python 如何使用 Selenium 和 ZA7F5F352426B628741B11 填写 https://www.nike.com/register 中的 Email 地址字段 - How to fill the Email address field within https://www.nike.com/register using Selenium and Python 如何使用 Selenium 和 Python 在 https://www.deepl.com/translator 中单击复制按钮 - How to click on the Copy button within https://www.deepl.com/translator using Selenium and Python 如何使用 Selenium 和 Python 在嵌套的 iframe 中定位元素 - How to locate an element within nested iframes using Selenium and Python 如何使用 Selenium 和 Python 在 xpath 中相对于变量定位元素 - How to locate an element with respect to a variable within a xpath using Selenium and Python How to select a country from https://www.aliexpress.com/ ship to drowdown menu using Selenium and Python - How to select a country from https://www.aliexpress.com/ ship to drowdown menu using Selenium and Python 如何使用Python和Selenium访问网站https://www.aliexpress.com/后点击并关闭横幅 - How to click and close the banner after accessing the website https://www.aliexpress.com/ using Python and Selenium 如何在Python中使用Selenium从https://www.amazon.in/的下拉列表框中选择选项Books? - How to select the option Books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python? 如何使用Selenium和Python在URL https://maps.mapmyindia.com/direction中单击文本为“ GET ROUTES”的元素? - How to click on element with text as “GET ROUTES” within the url https://maps.mapmyindia.com/direction using Selenium and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM