Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按标题查找元素时,找不到元素错误_Python_Selenium_Selenium Webdriver_Xpath - Fatal编程技术网

Python 按标题查找元素时,找不到元素错误

Python 按标题查找元素时,找不到元素错误,python,selenium,selenium-webdriver,xpath,Python,Selenium,Selenium Webdriver,Xpath,我正试图使用下面的代码找到使用标题的“将卡片添加到购物车”按钮。有人能帮我找出为什么找不到按钮吗 from selenium import webdriver import time from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import StaleElementReferenceException from selenium.common.excep

我正试图使用下面的代码找到使用标题的“将卡片添加到购物车”按钮。有人能帮我找出为什么找不到按钮吗

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import NoSuchFrameException

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options, executable_path=r'C:\Users\David\Desktop\Python\chromedriver_win32\chromedriver.exe')
url = 'https://store.cardsagainsthumanity.com/'
driver.get(url)
xpath = "//*[@title='Add Cards Against Humanity to cart']"
time.sleep(2)
print('Finding element')
iframes = driver.find_elements_by_xpath("//iframe")
for index, iframe in enumerate(iframes):
    print('Searching iframe ' + str(index))
    try:
        driver.switch_to.frame(index)
        try:
            element = driver.find_element_by_xpath(xpath)
            print(element)
        except StaleElementReferenceException:
            print('Stale element')
        except NoSuchElementException:
            print('Element not found on iframe index ' + str(index))
    except NoSuchFrameException:
        print('frame not found')
    driver.switch_to.parent_frame()
try:
    print('Element found', element)
except:
    print('Element not found')

有多个“添加到行李”按钮,您到底想要哪一个?给我们看一张图片或给出箱子的名称,以便更好地理解!几乎所有按钮都有iframe。驱动程序需要切换到特定的iframe,你想点击什么按钮?你需要发布一个。我们看不到
OpenBrowser()
正在做什么。将您的代码精简到重现问题所需的最少代码,我们可以运行这些代码来查看结果。好的,谢谢,我刚刚这么做了。我更新了代码,因为我目前正试图按照建议迭代所有的iframe,所以所显示的代码应该可以很容易地查看到底发生了什么。