Selenium 异常错误 - 元素在 (x,y) 点不可点击。其他元素将收到点击

我们可能会遇到 Selenium Exception Error - Element is not clickable at point(x,y)。其他元素会在工作 Selenium webdriver 时收到点击。

这通常是在从 Chrome 浏览器执行 Selenium 测试时看到的,而不是在其他浏览器(如 IE 和 Firefox)中。发生这种情况是因为 Chrome 浏览器无法计算网络元素的正确位置。

此外,在 Chrome 浏览器中,元素在其中间位置被点击。由于应用程序和 Selenium 之间发生同步问题,也可能遇到此异常。

存在一些解决此问题的解决方案,如下所示 -

  • 我们应该确保我们使用的是最新版本的 chromedriver,并且它与我们本地系统中的 Chrome 浏览器版本兼容。

  • 获取 webelement 的坐标,然后使用 Actions 类中的方法对其进行单击。

语法

WebElement elm = driver.findElement(By.tagName("input"));
//Point类的实例
Point location = elm.getLocation();
//得到 x, y 坐标
int m = location.getX();
int n = location.getY();
//Actions 类的实例
Actions a = new Actions(driver);
a.moveToElement(elm,m,n).click().build().perform();

  • 获取 webelement 的坐标并使用 JavaScript Executor 单击它。

获取 x 坐标的语法 -

WebElement l = driver.findElement(By.tagName("input"));
JavascriptExecutor j =(JavascriptExecutor)driver;
j.executeScript(
   "window.scrollTo(0,"l.getLocation().x+")");
l.click();

获取 y 坐标的语法 -

WebElement l = driver.findElement(By.tagName("input"));
JavascriptExecutor j =(JavascriptExecutor)driver;
"window.scrollTo(0,"l.getLocation().y+")");
l.click();