【转】Selenium 报错:Element is not clickable at point的解决办法

天一同学在写Selenium Java脚本时遇到一个问题,登录进入系统之后,要点击左侧的一个菜单,但是执行到该语句时报下面的错误:

Firefox中报错如下:
org.openqa.selenium.ElementClickInterceptedException: Element <div class="el-submenu__title"> is not clickable at point (115,358) because another element <div class="el-loading-mask is-fullscreen el-loading-fade-leave-active el-loading-fade-leave-to"> obscures it

错误的意思是:无法点击这个元素,因为被另一个div掩盖(obscure)住了。

Chrome 中报错如下:
org.openqa.selenium.WebDriverException: unknown error: Element <div class="el-submenu__title" style="padding-left: 20px;">...</div> is not clickable at point (115, 358). Other element would receive the click: <div class="el-loading-mask is-fullscreen el-loading-fade-leave-active el-loading-fade-leave-to" style="z-index: 2000;">...</div>
  (Session info: chrome=67.0.3396.99)

错误的意思是:无法点击这个元素,另外一个div元素接收了这个点击。

经分析调试,以下方法可以解决此类问题。
解决方法一:
思路:先使用invisibilityOf等待掩盖的div消失不见,再使用elementToBeClickable等待要点击的元素达到可点击的状态。

示例:

//要点击的左侧菜单元素
WebElement LeftMenu = driver.findElement(By.xpath("xpath"));

//掩盖的div元素
WebElement ObscureDiv = driver.findElement(By.xpath("//div[@class='el-loading-mask is-fullscreen el-loading-fade-leave-active el-loading-fade-leave-to']"));

//使用显示等待,等待掩盖的div消失
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.invisibilityOf(ObscureDiv));

//等待左侧菜单到可点击状态
wait.until(ExpectedConditions.elementToBeClickable(LeftMenu ));

//之后再执行点击
LeftMenu .click();
解决方法二:
思路:因为掩盖的div可能会在进行一些操作后,会消失,所以登录后执行一个页面刷新的操作,此div即可消失。

          再等待左侧菜单到可点击状态即可。

示例:

//登录之前的代码

//登录后加时间等待,并且进行一次页面刷新
Thread.sleep(3000);
driver.navigate().refresh();

//要点击的左侧菜单元素
WebElement LeftMenu = driver.findElement(By.xpath("xpath"));

//等待左侧菜单到可点击状态
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.elementToBeClickable(LeftMenu ));

//之后再执行点击
LeftMenu .click();

****************************************************************************************************

最近我会持续更新Selenium Java的相关文章,也请大家多多关注我的视频课程

全网最新、最完整、最具性价比、并且会持续保持更新的自动化测试课程

Selenium3 Java自动化测试完整教程
*****************************************************************************************************

 

关注火烈鸟测试公众号:huolieniaotesting,我会在上面更新一些关于测试的文章,我经常会逛一些国外的论坛,也会把比较好的国外专家的文章及国外测试动态分享到这个公众号里,让大家可以了解到国外的测试情况,也希望大家能在上面发表文章,聊聊测试那些事儿。

 

解决思路:

 

错误Element is not clickable at point (x, y)可能源于不同因素。您可以通过以下任一过程解决它们:

1.由于存在JavaScript或AJAX调用,元素未被点击

尝试使用ActionsClass:

WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2.元素未被点击,因为它不在视口

尝试使用JavascriptExecutor该元素在视口中:

WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement); 

3.在元素可点击之前,页面将被刷新。

在这种情况下,如第4点所述诱导ExplicitWaitWebDriverWait

4.元素存在于DOM中但不可点击。

在这种情况下, 请将ExplicitWaitExpectedConditions设置elementToBeClickable为可单击的元素:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));

5.元素存在但具有临时叠加。

在这种情况下,ExplicitWait使用 ExpectedConditionsset设置invisibilityOfElementLocated为Overlay是不可见的。

WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));

6.元素存在但具有永久叠加。

用于JavascriptExecutor直接在元素上发送单击。

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
posted on 2019-07-17 19:41  blogsheng  阅读(9504)  评论(0编辑  收藏  举报