首页 > 解决方案 > Selenium 无法在电子邮件模板中找到按钮

问题描述

我正在尝试使用 Selenium 在电子邮件模板中找到一个按钮并单击它,但无论我使用什么(xPath,css_selector),我的代码似乎都无法找到该按钮并不断收到此错误。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

电子邮件在 Mailinator 中,我可以访问 Mailinator 和电子邮件正文,但是当click操作即将执行时,我的代码无法抛出上述错误代码。

有没有人遇到过这个问题并且可以帮助我理解我做错了什么?如果您需要更多信息,请告诉我。非常感谢您的时间和帮助

标签: python-3.xseleniumselenium-webdriver

解决方案


貌似是iframe,如果是,需要先切换,再交互:

代码 :

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath here")))

然后你可以互动:

wait.until(EC.element_to_be_clickable((By.XPATH, "email field xpath here"))).send_keys('pass some email address')

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

完成后,切换到default content这样的:

driver.switch_to.default_content()

推荐阅读