如何在中找到“创建帐户”元素https://www.shopdisney.com/ 使用Selenium和Python
我一直在尝试用selenium和python单击上面的“创建帐户”按钮,但python似乎找不到该元素。这是我目前的代码:如何在中找到“创建帐户”元素https://www.shopdisney.com/ 使用Selenium和Python,python,selenium,xpath,iframe,css-selectors,Python,Selenium,Xpath,Iframe,Css Selectors,我一直在尝试用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"
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)
文件“skit.py”,第8行,在
link=driver.find_element_by_class_name('btn-group btn group create account ng scope'))
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/webdriver.py”,第564行,按类名称查找元素
返回self.find_元素(by=by.CLASS_NAME,value=NAME)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/webdriver.py”,第976行,在find_元素中
返回self.execute(Command.FIND_元素{
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/webdriver.py”,执行中第321行
self.error\u handler.check\u响应(响应)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/errorhandler.py”,第242行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:无此类元素:无法定位元素:{“方法”:“css选择器”,“选择器”:.btn组btn组创建帐户ng范围”}
(会话信息:chrome=83.0.4103.97)
我尝试使用不同的方法来识别元素,如XPath、css等,但我仍然无法找到它并单击它。我相信它与iFrame有关,但我不能完全确定。有人知道如何解决这个问题吗
谢谢!创建帐户时带有文本的链接位于
中,因此您必须:
- 诱导WebDriverWait以使所需帧可用并切换到该帧
- 诱导WebDriverWait以获得所需的
- 您可以使用以下选项:
:链接文本
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\u选择器
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
- 浏览器快照:
中,因此您必须:
- 诱导WebDriverWait以使所需帧可用并切换到该帧
- 诱导WebDriverWait以获得所需的
- 您可以使用以下选项:
:链接文本
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\u选择器
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
- 浏览器快照:
由于元素位于iframe中,您需要与之等效的webdriver:。顺便说一句,您尝试播放的特定iframe将产生错误:
Uncaught domeException:阻止了具有原点的帧”https://www.shopdisney.com"访问跨原点帧。
由于元素位于iframe中,您需要与此等效的webdriver:。顺便说一句,您尝试播放的特定iframe将产生错误:未捕获的DomeException:阻止具有原点的帧”https://www.shopdisney.com"从访问跨原点帧开始。
非常感谢!非常感谢!