Selenium webdriver 执行Selenium Web驱动程序脚本时出现错误“元素当前不可见,因此可能无法与之交互”

Selenium webdriver 执行Selenium Web驱动程序脚本时出现错误“元素当前不可见,因此可能无法与之交互”,selenium-webdriver,Selenium Webdriver,执行脚本的结果不一致。有时会通过,有时会抛出上面提到的异常。脚本结果不一样,因为元素在页面上出现的时间可能不同 使用sleep可以解决这个问题,但这并不是一个好的实践,因为如果测试用例在提供sleep之后失败,您可能需要调整/增加该值 更好地使用等待元素功能 有关详细信息,请参阅document@是,您需要使用条件等待,以便您的代码将等待,直到找到元素为止 例如:如果您使用的是web驱动程序 wait.untelexpectedconditions.visibilityOfElementLoca

执行脚本的结果不一致。有时会通过,有时会抛出上面提到的异常。

脚本结果不一样,因为元素在页面上出现的时间可能不同

使用sleep可以解决这个问题,但这并不是一个好的实践,因为如果测试用例在提供sleep之后失败,您可能需要调整/增加该值

更好地使用等待元素功能


有关详细信息,请参阅document@是,您需要使用条件等待,以便您的代码将等待,直到找到元素为止

例如:如果您使用的是web驱动程序


wait.untelexpectedconditions.visibilityOfElementLocatedBy.idusername

我建议尝试一下延迟,或者是短暂的睡眠,或者是waitforelement。至少是时候升级你的版本了。Selenium的v2.32和Firefox的v22?我们的版本是Selenium的v2.41和Firefox的v29。该升级了。你好,Arran,我使用的是Mozilla版本28.0和Selenium 2.32。如果我将它升级到新版本,我以前运行的脚本会成功运行吗?我不想在版本升级时出现新错误。请尽快告诉我。提前谢谢。Richard我已经在使用睡眠了,但它不起作用 org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 21 milliseconds Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:22:56' System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_25' Session ID: 16492097-714b-42c4-b9dc-2c3979db180f Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=22.0}]