Catch certain webdriver errors like StaleElementReferenceError
See original GitHub issueI’m using protractor intensively on a heavy angular app.
Sometimes i get random webdriver errors that depend on unknown factors like server speed at that time or browser processor cpu too high at one particular moment. They happen more often in IE10/IE9, less often in FF27/IE11 and almost never in Chrome 32.
My question/request is, if there is a way i can enclose some kind of try { } catch() block around webdriver errors like StaleElementReferenceError or NoSuchWindowError so i can recover and retry instead of failing the test for things that are more selenium-webdriver related than the application or protractor itself.
I’ve tried this but doesn’t catch the error probably because webdriver errors aren’t exposed as javascript exceptions:
// Fighting against StaleElementReferenceError
var retryFindIdxElementAndExpectTextContains = function(listElms, idx, subElm, subText, attempts) {
if (attempts == null) {
attempts = 3;
};
browser.driver.findElements(listElms).
then(function(elems) {
elems[idx].findElement(subElm).then(function(elm) {
try {
expect(elm.getText()).toContain(subText);
} catch(err) {
if (attempts > 0) {
retryFindIdxElementAndExpectTextContains(listElms, idx, subElm, subText, attempts - 1);
} else {
throw err;
}
}
});
});
};
Issue Analytics
- State:
- Created 10 years ago
- Reactions:2
- Comments:16 (12 by maintainers)
Top Results From Across the Web
Stale Element Reference Exception in Selenium Webdriver ...
StaleElementReferenceException. A stale element reference exception is a WebDriver error that is thrown in the following two cases.
Read more >How To Handle Stale Element Reference Exceptions In ...
This tutorial guides you through handling the stale element reference exception in Selenium using Java.
Read more >Top 10 Selenium Exceptions and How To Handle These ...
Handling Exceptions in Selenium WebDriver - In this tutorial we will learn about types of exceptions and how to handle top 10 most...
Read more >Stale element reference - WebDriver - MDN Web Docs
The stale element reference error is a WebDriver error that occurs because the referenced web element is no longer attached to the DOM....
Read more >Selenium WebDriver How to Resolve Stale Element ...
findElement call in a try-catch block and catch the StaleElementReferenceException , then you can loop and retry as many times as you need ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You should be able to use the promise error handling returned by
elm.getText()
. Tryelm.getText().then(function() { // passing case}, function(err) { // error handling here})
Stop using
findElement
in your page objects, replace for exampleWith
Do the same with the rest, retry and if still getting
StaleElementReferenceError
let us know.