0

我有以下片段

,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()方法的异常?

4

1 回答 1

2

在您的回调中,尝试使用remote而不是this.parent. this.parent使用与父链相同的上下文元素。这意味着,如果您因为尝试单击过时元素而最终被捕获,则调用this.parent.findById(id)catch执行基于该过时元素的搜索。

于 2017-04-20T14:30:58.350 回答