java - 令人沮丧的 ElementNotVisibleException 错误

标签 java selenium

当我试图点击一个按钮时出现以下错误:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

命令时长或超时:10.06秒

我尝试了以下方法,但都没有用:

1) 等待 10 秒,以防页面正在加载

2) 这样使用 JS 执行器:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", By.cssSelector("#IconButton > input.IconButtonDisplay"));

3) 使用等到元素可见

实际上执行了第 2 个,但点击的结果没有发生,即新页面没有打开。

第 3 次说明按钮不可见,但按钮可见且可以手动点击。

我可以告诉你的是,使用 Selenium IDE 我可以播放并点击按钮没有问题。

按钮的HTML(不能在这里放太多作为专有信息)。格式化道歉:

<div widgetid="dijit__WidgetsInTemplateMixin_13" id="dijit__WidgetsInTemplateMixin_13" class="gridxCellWidget">
  <div class="IconButton" widgetid="IconButton" id="IconButton" data-dojo-type="ab.cd.ef.gh.IconButton" data-dojo-attach-point="rowBtn1Pt">
    <input class="IconButtonDisplay" src="/tswApp/ab/cd/ef/gh/images/edit.png" style="width: 20px;" type="image">
  </div>
</div>

最佳答案

在 Javascript 执行器中,您希望传递 WebElement 的实例而不是 By 选择器。所以改变

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();",   By.cssSelector("#IconButton > input.IconButtonDisplay"));

WebElement element = driver.findElement(By.cssSelector("#IconButton > input.IconButtonDisplay"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

关于java - 令人沮丧的 ElementNotVisibleException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239864/

相关文章:

python - 如何使用 python 查找 iframe 中按钮的 XPath?

javascript - 相同的选择器在常规浏览器和 Selenium 中给出不同的结果

java - 如何使用 Selenium 修复 Edge 浏览器中的 ElementNotVisibleException?

java - 如何使用远程驱动程序从 Selenium 2 gird 中的输入 html 标记中选择文件

java - 在 Liferay 6.2 中,“站点设置”中的“保存”按钮不起作用

java - GlazedList 更新 JTable 中的 EventList

c# - 从日期时间到当前时区的 Unix 纪元时间的转换

java - 如何在JavaFX中绑定(bind)到复合属性的组件?

java - 单击按钮时显示或隐藏部分面板的 Swing 方法?

java - 线程 "main"org.openqa.selenium.NoSuchElementException : When trying to select an element from a pop-up using selenium 中的异常