question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catch certain webdriver errors like StaleElementReferenceError

See original GitHub issue

I’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:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

7reactions
juliemrcommented, Feb 27, 2014

You should be able to use the promise error handling returned by elm.getText(). Try

elm.getText().then(function() { // passing case}, function(err) { // error handling here})

1reaction
elgalucommented, Oct 3, 2014

Stop using findElement in your page objects, replace for example

this.username = ptor.findElement(protractor.By.css(‘input[name=“email”]’));

With

this.username = $('input[name=email]'); // or element(by.css('input[name="email"]')) if you're verbose

Do the same with the rest, retry and if still getting StaleElementReferenceError let us know.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found