英文:
Unable to locate element: {"method":"xpath","selector":"//div[@class='landing__actions']//a"}
问题
我有这个网站,尝试在Selenium Python中点击一个按钮,但它一直告诉我无法定位到元素,无论是隐式等待还是显式等待都无法使其工作。请提供任何指导将不胜感激。
以下是我所做的事情:
import time
import unittest
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.wait import WebDriverWait
class TestButton(unittest.TestCase):
def test_button(self):
self.driver = webdriver.Chrome()
self.driver.get('https://www.moneyhelper.org.uk/en/money-troubles/coronavirus/use-our-money-navigator-tool')
time.sleep(3)
"""点击关闭弹出窗口"""
self.driver.find_element(By.XPATH, '//*[@id="ccc-notify-accept"]').click()
time.sleep(3)
"""我一直在尝试点击的按钮,它是一个锚标签"""
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
英文:
I have this website am trying to click on a button in selenium python but it keeps telling me it cant locate the element both implicitly and explicit wait could not make it work. Please any guidance would be appreciated.
Below is what i have done:
import time
import unittest
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.wait import WebDriverWait
class TestButton(unittest.TestCase):
def test_button(self):
self.driver = webdriver.Chrome()
self.driver.get('https://www.moneyhelper.org.uk/en/money-troubles/coronavirus/use-our-money-navigator-tool')
time.sleep(3)
"""clicking to close a cooking popup"""
self.driver.find_element(By.XPATH, '//*[@id="ccc-notify-accept"]').click()
time.sleep(3)
"""the button which is an anchor tag i have been trying to click"""
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
答案1
得分: 1
有一个 iframe 在 //div[@class='landing__actions']//a
之前,你需要切换到这个 iframe
只需尝试:
"""点击关闭弹出的烹饪窗口"""
self.driver.find_element(By.XPATH, '//*[@id="ccc-notify-accept"]').click()
time.sleep(3)
iframe = self.driver.find_element(By.XPATH, '//iframe[@title="Money Navigator tool"]')
self.driver.switch_to.frame(iframe)
"""我一直在尝试点击的按钮,它是一个锚点标签"""
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
英文:
There is an iframe before //div[@class='landing__actions']//a
and you need to switch to this iframe
just try it:
"""clicking to close a cooking popup"""
self.driver.find_element(By.XPATH, '//*[@id="ccc-notify-accept"]').click()
time.sleep(3)
iframe = self.driver.find_element(By.XPATH, '//iframe[@title="Money Navigator tool"]')
self.driver.switch_to.frame(iframe)
"""the button which is an anchor tag i have been trying to click"""
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
答案2
得分: 0
这在一个 iframe
中,你需要先切换到它,使用 .switch_to.frame(iframe 参考)
:
...
iframe = self.driver.find_element(By.CSS_SELECTOR, '.cmp-embed__iframe')
"""我一直在尝试点击的按钮,它是一个锚点标签"""
self.driver.switch_to.frame(iframe)
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
英文:
It's inside a iframe
, you need switch it first using .switch_to.frame(iframe reference)
:
...
iframe = self.driver.find_element(By.CSS_SELECTOR, '.cmp-embed__iframe')
"""the button which is an anchor tag i have been trying to click"""
self.driver.switch_to.frame(iframe)
self.driver.find_element(By.XPATH, "//div[@class='landing__actions']//a").click()
time.sleep(5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论