Selenium (Python) 错误:元素不可交互

发布于 2025-01-11 22:11:38 字数 588 浏览 0 评论 0原文

我试图从该网站单击此特定的 Web 元素: https: //www.milanofinanza.it/quotazioni/ricerca/listino-completo-2ae?refresh_cens

在此处输入图像描述

我尝试了很多不同的方法,但仍然遇到相同的错误(元素不可交互)。

我的代码如下所示:

wd.find_element(By.XPATH,"//*[@id='mainbox']/div[2]/div[2]/div[4]/div/div[1]/div/button[4]").click()

该元素实际上已被检测到,但由于某种原因它不可单击。

I am trying to click on this particular web element from this site: https://www.milanofinanza.it/quotazioni/ricerca/listino-completo-2ae?refresh_cens

enter image description here

I have tried in many different ways but i still get the same error (element not interactable).

My code looks like this:

wd.find_element(By.XPATH,"//*[@id='mainbox']/div[2]/div[2]/div[4]/div/div[1]/div/button[4]").click()

The element is actually detected but for some reason it isn't clickable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2025-01-18 22:11:38

我可以想到两种可能的解释:

  1. 您检测到的元素不是您认为检测到的元素。您使用的是相当长的 XPATH,因此如果没有外部确认您确实选择了您想要的按钮,我会怀疑。如果您选择的按钮被禁用,那么它将无法交互。
  2. 该按钮可能无法通过 selenium 的 .click() 期望的相同方法点击。我不熟悉 ng- 属性,所以也许这只是语言本地化的事情,但我怀疑 selenium 可能正在寻找 onclick 属性。

我的建议:

  1. 不要使用复杂的 XPATH,而是考虑通过按钮的内部文本进行选择。所以像这样: //*button[text()=Successiva]
  2. 根本不使用任何 xpath,只需调用按钮中引用的 JavaScript 函数: getDataTableNextClick()< /代码>

I can think of two possible explanations:

  1. The element you detected is not the element you think was detected. You're using a rather long XPATH, so I would be suspicious without external confirmation that you really selected the button you wanted. If the button you did select is disabled, then it will not be interactable.
  2. The button might not be clickable through the same method that selenium's .click() expects. I am not familiar with ng- attributes, so maybe it's just a language localization thing, but I suspect that selenium may be looking for an onclick attribute.

My suggestions:

  1. Instead of using a complicated XPATH, consider selecting by the inner text of the button. So something like this: //*button[text()=Successiva]
  2. Instead of using any xpath at all, just call the JavaScript function that is referenced in the button: getDataTableNextClick()
杯别 2025-01-18 22:11:38
wait=WebDriverWait(driver,20)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
except:
    pass

要简单地单击该元素,只需查找带有该文本的按钮即可。

要浏览 15 个页面,您可以这样做

current=1
last=15
while True:
    try:
        if current == last:
            break
        current+=1
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
    except:
        break

现在,如果您想浏览 15 个页面,此网站会有点棘手,因为分页不会禁用该按钮。

current="1"
old="2"
while True:
    try:
        if current == old:
            break
        old=current
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
        current=wait.until(EC.visibility_of_element_located((By.XPATH,"//*[contains(@class,'page-item ng-scope active')]"))).text
    except:
        break

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
wait=WebDriverWait(driver,20)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
except:
    pass

To simply click on that element just look for the button with that text.

To go through 15 pages you could do

current=1
last=15
while True:
    try:
        if current == last:
            break
        current+=1
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
    except:
        break

Now this site is a little trickier if you want to go pass the 15 pages since the pagination doesn't disable the button.

current="1"
old="2"
while True:
    try:
        if current == old:
            break
        old=current
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
        current=wait.until(EC.visibility_of_element_located((By.XPATH,"//*[contains(@class,'page-item ng-scope active')]"))).text
    except:
        break

Import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文