I have the following snippet
,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()
;
}
which is supposed to handle StaleElementReferenceException
, or any other for that matter, and try to find the element again and click on it. The element is added and removed to/from the dom on a fixed interval and therefore sometimes i get this exception in my test run and therefore a failed run. I want to handle this exception and prevent the run from failing as is not really failing due to a bug (or is it?).
So the question is How do I handle an exception on the .click()
method?
In your callback, try using remote
instead of this.parent
. this.parent
uses the same context element as the parent chain. This means that if you ended up in the catch because you tried to click a stale element, calling this.parent.findById(id)
in the catch
will be performing a search rooted on that stale element.