Java 令人沮丧的ElementNotVisibleException错误
当我尝试单击按钮时,出现以下错误:Java 令人沮丧的ElementNotVisibleException错误,java,selenium,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)
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 executor中,您希望传递WebElement的实例,而不是按选择器传递。所以改变
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", By.cssSelector("#IconButton > input.IconButtonDisplay"));
到
你试过只用FirefoxDriver吗
FirefoxDriver driver = new FirefoxDriver();
你试过用这个吗?这还不够独特吗
driver.findElement(By.cssSelector("input.IconButtonDisplay")).click();
如果没有,那么试试这个(它与您使用JSE所做的相同)
也许不是输入引起了点击?您是否尝试单击任一父div
driver.findElement(By.id("IconButton")).click();
或
对我来说,这就是成功的原因:
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver,"triggerMouseEventAt", editButton,"click", "0,0");
希望这能帮助其他有同样问题的人。您能提供任何可重复的方案吗?抱歉,Saifur,在我完成问题更新之前,我单击了post!:-(忘了提及,一旦测试失败,我也可以手动单击按钮而不出现任何问题。请根据需要提供元素的HTML以及可能的周围HTML。为什么您使用JavascriptExecutor而不仅仅是driver.findelelement(By.cssSelector())?该错误意味着该元素对用户不可见。您确定使用了正确的元素吗?我使用JSE作为驱动程序。findElementBy是元素不可见异常的失败原因。添加了一些HTML。我尝试了您的建议,但遇到了与之前相同的问题,即它执行脚本,但实际的按钮单击没有发生。
driver.findElement(By.id("IconButton")).click();
driver.findElement(By.id("dijit__WidgetsInTemplateMixin_13")).click();
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver,"triggerMouseEventAt", editButton,"click", "0,0");