Protractor alert: Stale element detected when using "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"

I am facing a major issue right now. The stale element reference problem occurs 7 out of 10 times in my application. Once the operation is completed, a confirmation dialog appears momentarily. I need to retrieve the text from this confirmation dialog.

var confDial= element(by.id('dialog'));
browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);
confDial.getText().then(function(text1){
      expect(text1).toBe(TextExpected);
});

My approach involves waiting for the dialog to be visible after the operation and then extracting the text using getText(). However, it fails at the browser.wait() step with the following error:

Failed: stale element reference: element is not attached to the page document 09:14:52 (Session info: chrome=52.0.2743.116) 09:14:52 (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 4.2.0-42-generic x86_64) (WARNING: The server did not provide any stacktrace information) 09:14:52 Command duration or timeout: 76 milliseconds 09:14:52 For documentation on this error, please visit:

Could anyone advise me on how to resolve this issue?

Appreciate your help in advance.

Answer №1

From what I understand, the visibilityOf(element) function requires the element to be present first. Therefore, it is important to wait for the element to become present before checking its visibility.

You can try the following approach:

browser.wait(EC.presenceOf(element(by.id('dialog'))), FIFTY_SECONDS);
var confDial = element(by.id('dialog'));
browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);
// expect statement already resolves a promise, so no need for a then() here
expect(confDial.getText()).toBe(TextExpected);

//confDial.getText().then(function(text1){
//      expect(text1).toBe(TextExpected);
//});

Alternatively, you could try this (although not certain if it will work):

browser.wait(EC.presenceOf(var confDial = element(by.id('dialog'))), FIFTY_SECONDS);

Answer №2

Have you tested the isPresent method?

This function checks whether an object exists in the DOM tree...

confDial.isPresent().then(function(result) {
    if ( result ) {
       confDial.getText().then(function(text1){
        expect(text1).toBe(TextExpected);
       });
    } 
    else {
   //performing an alternative action  
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I change the "Return" button on the iOS keyboard to a "Next" button using jQuery or JavaScript?

Currently, I am developing an HTML application and working on implementing it for IOS devices such as the IPAD. Within my application, there are multiple text boxes. Whenever a user clicks on a text box to input text, the keypad appears. On this keypad, ...

How can I extract information from an HTML element using its ID in JavaScript?

Can anyone please help me retrieve the date using JavaScript from the following div: <div id="popupDateFieldMin" class="dateField">2017-08-02</div> Below is the code snippet I am currently using: var cal = document.getElementById( 'popu ...

Determining the total number of documents through aggregation while implementing skip and limit functionality

Is there a way to retrieve the total count of documents when implementing aggregation along with limit and skip in a query similar to the one below? db.Vote.aggregate({ $match: { tid: "e6d38e1ecd", "comment.top ...

"Learn how to extract the image URL from the configuration file (config.json) within the assets folder, and then seamlessly display it within

In my Angular project, I have a configuration file located in the assets folder: { "brandConfig": "brand1", "brand1": {"urlPath": "http://192.168.168.60:8081/mantle-services", " ...

Alter the size of an element's width dynamically while maintaining a predetermined width

Having a fixed width div: // CSS @media only screen .column.small-centered:last-child, .columns.small-centered:last-child { float: none; } .lesson_lt_mc_div .small-centered { margin-top: 15px; margin-bottom: 15px; width: 296px; } foundation.css:1 ...

Creating nested namespaces with interfaces in Typescript type definitions

In my Javascript project, I am trying to define typing for a specific structure. Consider the following simplified example: a | + A.js + b | + B.js Here we have a folder 'a', and inside it there is another folder 'b'. My goal is t ...

Employing a form for initiating a JavaScript function

I need help running a JavaScript method with a custom text variable as a parameter. My goal is to input some text in a form and then pass that value to the method for execution. However, I'm encountering an issue where it seems like the method is rece ...

Selenium: Unable to locate DOM element by name with @FindBy method

I'm having trouble finding the DOM Element using Selenium. The element in the DOM is named "AddBeverageBtn" This is my Page class: public class MainPage extends BasePage { @FindBy(name="AddBeverageBtn") private WebElement buttonAdd; Unfortu ...

What is the best way to incorporate a URL link in Java while utilizing a CSS element?

Below is the provided code snippet: <span class='maincaptionsmall'> Test </span> Due to certain constraints in my page related to ajax and jquery scripts, I am unable to use HREF as a link. In other words, I can't implement som ...

Having trouble with the Cordova button not functioning on Android 9? A beginner seeks assistance

I attempted to create a button with the id of "clickme" that links to index.html and js/buttonexample.js. Although I could see the button on both the browser and android emulator, it wasn't functioning as expected when clicked. js/buttonexample.js d ...

An error occurred during Selenium automation testing in Java using ChromeDriver Chrome due to org.openqa.selenium.NoSuchSessionException: no such session error

Encountering a "No such session exception" error while running Selenium in my pipeline. I have tried several solutions but none seem to work for me. Any assistance or ideas would be greatly appreciated. Exception trace: org.openqa.selenium.NoSuchSessionEx ...

Display a div in jQuery when a specific radio button is selected

I have set up three radio buttons to display three different divs, but the functionality is not working correctly. Below is the script I am using: <script> $(document).ready(function() { $("input[name$='Want_To']").click(function() { ...

Learn a valuable trick to activate CSS animation using a button - simply click on the button and watch the animation start over each time

I already know how to do this once. However, I would like the animation to restart or begin again when the user clicks on it a second time. Here is what I have: function animation() { document.getElementById('ExampleButton').className = &apo ...

Click on the image to view in a separate window

I am looking for assistance with a script that can open an image in a new page, not a new window. I have a specific HTML page designated for displaying various images when clicked on. If anyone has any ideas or guidance on how to achieve this, it would be ...

The Firebase promise resolves before the collection is refreshed

Currently utilizing AngularFire for my project. I wrote some code to add a new record to an array of records and planned on using the promise then function to update the array with the most recent datestamp from the collection. this.addRecord = function() ...

Create a new object in Three.js every x seconds and continuously move each object forward in the Z-axis direction

I am currently developing a Three.js endless runner game where the player controls a character dodging cars on a moving road. At this early stage of development, my main challenge is to make the hero character appear to be moving forward while creating the ...

How come the instanceof operator returns false for a class when its constructor is present in its prototype chain?

While working on a NodeJS app, I encountered unexpected behavior when trying to verify that a value passed into a function is an instance of a specific Class. The issue arises when using instanceof between modules and checking the equality of the Class. e ...

How can one get rid of a sudden strong beginning?

Recently, I delved into the world of CSS animation and encountered a frustrating issue. I've tried numerous methods and workarounds to achieve a smoothly looping animation without interruptions. Despite my efforts, I have not been able to replicate th ...

Is it possible for me to invoke an anonymous self-executing function from a separate scope?

I'm having an issue with calling the hello() function in ReactJS. When I try to call it, I receive an error message that says "Uncaught ReferenceError: hello is not defined". How can I go about resolving this error? This is a snippet from my index.ht ...

Ways to create dynamic functionality with JavaScript

I need to iterate through the document.getElementById("b") checked in a loop. Is this achievable? How can I implement it? <img class="img-responsive pic" id="picture" src="images/step1.png"> <?php //get rows query ...