「Selenium」- Element XXX is not clickable at point (672, 582)

  CREATE BY JENKINSBOT

问题描述

在使用 Selenium 进行自动化测试时,产生如下错误:

Caught: org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span style="display: inline-block;">...</span> is not clickable at point (672, 582). Other element would receive the click: <div class="garr-footer-publish-content publish-footer-content">...</div>
   (Session info: chrome=87.0.4280.66)
 Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
 System info: host: 'laptop-k53sd', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-47-generic', java.version: '1.8.0_265'
 Driver info: org.openqa.selenium.remote.RemoteWebDriver
 Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.66, chrome: {chromedriverVersion: 87.0.4280.66 (fd98a29dd59b3..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:45673}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: a2d23b1395ccec0af3e229a7c49...}
 Session ID: a2d23b1395ccec0af3e229a7c4915747
 org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span style="display: inline-block;">...</span> is not clickable at point (672, 582). Other element would receive the click: <div class="garr-footer-publish-content publish-footer-content">...</div>
   (Session info: chrome=87.0.4280.66)
 Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
 System info: host: 'laptop-k53sd', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-47-generic', java.version: '1.8.0_265'
 Driver info: org.openqa.selenium.remote.RemoteWebDriver
 Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.66, chrome: {chromedriverVersion: 87.0.4280.66 (fd98a29dd59b3..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:45673}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: a2d23b1395ccec0af3e229a7c49...}
 Session ID: a2d23b1395ccec0af3e229a7c4915747
...

错误信息的主要内容,集中在第一行,我们重点关注第一行即可。

问题原因

经过查找,有以下几种原因将导致该问题(也可能还有其他原因):
1)在当前页面中,元素不可见,所以无法点击;
2)在点击前,页面发生刷新,或者其他原因使元素不可见,导致无法点击;
3)元素可点击的,但是有其他元素覆盖在其上,导致无法点击;

解决方案

针对我们遇到的问题,我们发现在浏览器的 Console 中,可以通过 click() 方法进行点击。因此,我们使用 Selenium 直接执行 JavaScript 代码来点击按钮,如下示例:

((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", elementToClick);
// elementToClick 是需要点击的元素

如果元素不可见,即不在当前 ViewPort 中,则需要先进行滚动。如下两种方法:

// 执行 JavaScript 代码
((JavascriptExecutor) webDriver).executeScript('arguments[0].scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});', elementToShow);

// 通过 Action 滚动到元素
Actions action action = new Actions(webDriver);
action.moveToElement(elementToShow).perform();

参考文献

google chrome – Debugging “Element is not clickable at point” error – Stack Overflow