我实施了很多Selenium测试。有时,我的测试由于StaleElementReferenceException
而失败,所以我想检查这种方法:
点击处理:
def clickHandle(expectedCondition: (By) => ExpectedCondition[WebElement], by: By, timeOut: Long): Unit = {
var count = 0
var isClicked = false
breakable {
while (count < 4 || !isClicked) {
try {
val element = driver.findElement...
element.click()
isClicked = true
break
} catch {
case e: StaleElementReferenceException => println("Trying to recover from a stale element")
count = count + 1
}
}
}
}
获取属性句柄:
def getAttributeHandle(expectedCondition: (By) => ExpectedCondition[WebElement], by: By, timeOut: Long, attribute: String): String = {
var count = 0
var result = false
var str = ""
breakable {
while (count < 4 || !result) {
try {
val element = driver.findElement...
str = element.getAttribute(attribute)
result = true
break
} catch {
case e: StaleElementReferenceException => println("Trying to recover from a stale element")
count = count + 1
}
}
}
str
}
答案 0 :(得分:-1)
通常当您收到StaleElementReferenceException时,它是页面加载错误。您需要做的就是添加一个页面等待文本值或页面上的任何内容,或者您可以暂停,例如:
sleep(10)
我使用ruby所以不是100%确定如何提供帮助,但一般来说,当我遇到这个时我会做什么是添加睡眠或调试代码并添加断点并逐步执行代码以查看它在哪个点处断开。