gpt4 book ai didi

javascript - 如何在 intern JS 中捕获异常?

转载 作者:行者123 更新时间:2023-12-03 04:30:20 25 4
gpt4 key购买 nike

我有以下代码片段

,clickStaleElement: function(remote, id) {
return remote
.findById(id)
.execute(function(id){
//force org.openqa.selenium.StaleElementReferenceException
$(document.getElementById(id)).addClass('hidden');
},[id])
.click()
.catch(function(){
return this.parent
.findById(id)
.execute(function(id){
//bring back the element
$(document.getElementById(id)).removeClass('hidden');
},[id])
.click()
.end()
;
})
.end()
;
}

它应该处理StaleElementReferenceException,或任何其他与此相关的内容,并尝试再次找到该元素并单击它。该元素以固定的时间间隔添加到 dom 或从 dom 中删除,因此有时我在测试运行中会遇到此异常,因此运行失败。我想处理此异常并防止运行失败,因为实际上并不是由于错误而失败(或者是吗?)。

所以问题是如何处理 .click() 方法上的异常?

最佳答案

在回调中,尝试使用 remote 而不是 this.parentthis.parent 使用与父链相同的上下文元素。这意味着,如果您因尝试单击过时元素而陷入捕获,则在 catch 中调用 this.parent.findById(id) 将执行搜索 Root 于那个陈旧的元素。

关于javascript - 如何在 intern JS 中捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43518845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com