【问题标题】:Selenium Error: Element is not clickable at point(X,Y), other element would receive the clickSelenium 错误:元素在点 (X,Y) 处不可点击,其他元素将收到点击
【发布时间】:2025-12-17 12:10:02
【问题描述】:

我正在使用 python 和 selenium。在this 链接上,我会点击Reply 添加评论

元素在点 (933.9500122070312, 16.666671752929688)。其他元素会收到点击:<a href="/create_account"></a>

此处给出的代码:

导入请求 从 bs4 导入 BeautifulSoup from gensim.summarization 导入总结

从硒导入网络驱动程序

from datetime import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from time import sleep     
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

    driver.get(url)
    sleep(4)
    f = driver.find_element_by_css_selector('.PostFull__reply')
    f.location_once_scrolled_into_view
    f.click()

更新

这是它在 Inspector 中的显示方式:

【问题讨论】:

  • 您要查找哪个元素? PostFull__reply 不在页面上
  • @YuZhang 问题已更新为图片
  • 您登录帐户了吗?
  • 是的。顺便说一句,如果你没有登录,链接就会出现

标签: python selenium


【解决方案1】:

您的 css 是正确的,长内容的问题是它会将元素滚动到 .Header 的下方,这就是它无法单击元素的原因。

由于.Header的高度是49.5px,所以你可以得到元素的位置并滚动到比Y坐标小100px,最好在测试前最大化窗口。见下文:

driver.maximize_window()
f = driver.find_element_by_css_selector('.PostFull__reply')
location = f.location["y"] - 100
driver.execute_script("window.scrollTo(0, %d);" %location)
f.click()

【讨论】:

  • 我已经在使用可见的 FireFox,所以猜想不需要截图。所需的 CSS 无法跨站点运行。如果帖子有很多文字,那么它就不起作用。没有弹出窗口,但是正在通过 JS 生成元素
  • 它适用于随机帖子。试试这个,它不会工作:steemit.com/steem/@bloggersclub/…
  • @Volatil3 检查我更新的答案,对于长内容页面,它会将其滚动到 .Header 下,这就是为什么无法与之交互的原因。
  • 如果你在失败时截图,你可以看到没有“重播”按钮,因为标题覆盖了它。 find_element_by_css_selector 函数从 0 开始索引。
  • @Volatil3 在你的情况下它应该大于 49,5 并且不大于屏幕尺寸。
【解决方案2】:

您正在尝试单击 span 元素,而实际上它应该是 a 元素。在这里,您应该尝试使用WebDriverWait 定位a 元素,等到元素在DOM 上可见且可点击,然后执行如下点击:-

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

driver.get(url)

f = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".PostFull__reply > a")))
f.location_once_scrolled_into_view
f.click() 

如果不幸的是f.click() 无法正常工作,您也可以使用execute_script() 执行点击,如下所示:-

driver.execute_script('arguments[0].click()', f)

希望它有效..:)

【讨论】: