Selenium Java Actions报错:TypeError: rect is undefin

2020-09-21 14:26发布

在火狐浏览器中使用Selenium Java 中的Actions类模拟鼠标操作时,遇到如下报错:

org.openqa.selenium.WebDriverException: TypeError: rect is undefined
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'USERCHI-66SNOKV', ip: '192.168.0.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.1'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

该脚本使用Actions类连续模拟了两次鼠标指上菜单元素的操作,一次是鼠标指上展开一级菜单,下一次是在展开的二级菜单中鼠标指上某个菜单并进行点击选择,示例如下:

Actions action = new Actions(driver);action.moveToElement(WebElement1).perform();action.moveToElement(WebElement2).click().perform();

原因可能是因为一级菜单指上后,展开二级菜单需要一个渲染的过程,需要时间等待,但是脚本直接执行第二个操作,就报了rect is undefined的错误。

在本人的例子中使用如下方法解决了此问题。

解决办法添加显示的时间等待,等待到第二级菜单完全展现并且可以点击再执行Action相关操作。

解决示例如下:

Actions action = new Actions(driver);action.moveToElement(WebElement1).perform();//加显示等待解决此问题WebDriverWait wait = new WebDriverWait(driver, 60);wait.until(ExpectedConditions.elementToBeClickable(WebElement2));	action.moveToElement(WebElement2).click().perform();

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

文章来源:CSDN 作者:YOYO测试

来源地址:https://blog.csdn.net/yoyocat915/article/details/87168021