Validating Disappearing Alert with Selenium WebDriver

Imagine a scenario where you fill out all the necessary information on a form, click Save, and then receive a message confirming that the form was saved successfully. The message pops up for about 6-7 seconds before fading away. Here is the HTML code for the message:

<div data-notify="container" class="alert-success animated" role="alert" data-notify-position="top-right">
    <span data-notify="title">
        <span class="glyphicon glyphicon-ok">::before</span>
        <span>Saving</span>
        <span data-modify="message">Successfully saved</span>
    </span>
</div>

You try to verify the text "Successfully saved" with the following code:

String message = driver.findElement(By.cssSelector("span.glyphicon glyphicon-ok")).getText();
assertTrue(message.contains("Successfully saved"));

However, this assertion fails because the element cannot be located.

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.glyphicon glyphicon-ok"}

You are unsure whether your code is incorrect or if the message fades away too quickly for the driver to find the element.

Any assistance or suggestions for correction would be greatly appreciated.

Thank you, Danny

Answer №1

Check out the code snippet below for a solution:

WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-notify='container']/descendant::span[@data-modify='message']")));
String message = el.getText();
assertTrue(message.contains("Successfully saved"));

I hope this helps you with your task. Feel free to ask if you need further assistance!

Answer №2

When dealing with modals, this is the approach I take. Make sure to insert this code snippet after the event that triggers the modal.

WebDriverWait wait = new WebDriverWait(driver, 3);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.alert-success")));   
WebElement message = driver.findElement(By.xpath("//span[@data-modify='message']"));
Assert.assertEquals("Successfully saved", message.getText());

Answer №3

Assuming that the indentation of your HTML code is correct as provided by you, I recommend trying out the following code:

// Pause here for a moment
String message = driver.findElement(By.cssSelector("div.alert-success.animated>span>span:nth-child(3)")).getText();
printn("The message is " +message);
assertTrue(message.contains("Successfully saved"));

Please update me on what the outcome is.

Answer №4

Thank you all for your patience and assistance in trying to help me out. I finally resolved the issue by using this code snippet. It was inspired by Efx's code, but I made some modifications to fit my needs.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(@class, 'col-xs-11 col-sm-4 alert alert-success animated fadeInDown fadeOutUp')]")));   
assertTrue(el.getText().contains("Successfully saved"));

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

The Power of ReactJS Spread Syntax

Currently working with React. In the state, I have an array of objects. this.state = { team: [{ name:'Bob', number:23 }, { name:'Jim', number:43 }] } My issue arises when attempting to create a copy of the arr ...

Using jQuery to load content with a dynamic variable instead of specific element IDs

I am facing a minor jQuery issue. I am trying to load a specific area of an external HTML document into a div, but instead of loading just that particular area, the entire document is being loaded. Here's the code snippet for the trigger: $('a& ...

Browser refresh not triggering view update in Rails and ReactJS application

Recently, I integrated Reactjs into my Rails application. Strangely, when I modify the content of a .jsx file and reload it with different text, such as changing from <h1>Hello<h1/> to <h1> Hello again<h1/>, the browser fails to res ...

JSF - The f:ajax execute functionality is failing to properly update the bean's backing property

I am facing an issue with two components on my page - a commandButton and a selectOneListbox. After clicking on the commandButton, I need to update the value of the backing property binded to the selectOneListBox. Both components are enclosed within the ...

Utilizing HTML and jQuery to load a dynamic URL within a div placeholder

I'm experiencing some difficulty with loading a custom URL. Essentially, the user will click on a series of navigation links that will dynamically load the relevant content in a tabbed bootstrap jumbotron. The navigation links vary based on data store ...

Differentiate the selected level on the DOM

Can someone help me with the following selector? $('#navigation ul li a').click(function(evt) {} It gets the elements I need, but also retrieves child elements. For example, it also selects: #navigation ul li li a // extra li I am curious a ...

Ways to determine the visibility status of an HTML tag in Angular 2

Is there a way to check if a div is hidden in Angular 2? ...

Error Encountered When Linking MySQL Database with NetBeans

Encountering an issue while running my code in NetBeans to check if mySQL is connected. Here's the problematic code: public static void main(String[] args) { Connection connect = null; try{ connect = DriverManager.getConnection("jdbc ...

What is the best way to showcase the output of a Perl script on a webpage

I recently came across a resource discussing the process of executing a perl script from a webpage. What is the best way to run a perl script from a webpage? However, I am facing a situation where the script I have takes more than 30 seconds to run and d ...

Using Javascript to save content to a text file across multiple lines

I am working on a coding project that involves extracting data from a JSON file via a URL and parsing the JSON content. The script successfully filters out the necessary data, but now I want to write this data to a text file with each piece of information ...

What is the process for incorporating Maven dependencies like Volley and Gson into a Bazel configuration for an Android development endeavor?

Currently, I am implementing the following code: deps = [ ":tensorflow_native_libs", "//tensorflow/contrib/lite/java:tensorflowlite", "@androidsdk//com.android.support:appcompat-v7-25.0.0", "@androidsdk//com.android.volley:volley:1.1.0", "@ ...

No values are being assigned to the array elements in the Node.js application

I've been struggling with an issue in my Express project for a day now. I can't seem to push some data into an array element. Let me walk you through my code and the data involved. Here is the result data retrieved from MongoDB: result = { n ...

Does having a different identifier for an object in Eclipse indicate that it is a separate instance while debugging?

Currently, I'm debugging an Android project using Eclipse. Interestingly, I have noticed that there are two distinct IDs assigned to a class variable. Could this imply that both IDs actually refer to separate instances of the class? ...

Is Consistency Possible with CSS Transition Direction?

Take a look at this example: https://codepen.io/jon424/pen/XWzGNLe In the provided code snippet, there is a button that can toggle the visibility of an image. When clicked, the image disappears from bottom to top and reappears when clicked again. The use ...

Currently, I am utilizing Selenium with Python to update the value of two WebElements. However, I am encountering a limitation where I am only able to update the value for

The snippet below shows the source code of this page. <tr> <th>Model Name<span class="c-c60000 txtNormal">*</span></th> <td><table cellpadding="0" cellspacing="0" border="0"><tr><td> ...

Managing pop-ups in browsers that disappear as soon as we click on them - a guide

Currently, I am in the midst of automating an ecommerce website with selenium. Upon completing the registration process on our site, a confirmation pop-up appears. Unfortunately, I have been unable to inspect this element as it disappears when I click an ...

Navigate to the specified location using AJAX

When I update a comment using AJAX, I want to automatically scroll down to the updated comment: $("#aggiorna").click(function(){ var value = $("#id").val(); var dato = $("#comment_edit").val(); var dato1 = $("#user_id").val(); var dato2 = ...

A guide on organizing and categorizing data by name with angularjs

Presented here is a list of descriptions associated with specific names. I am seeking guidance on how to group or list the descriptions by name. html: <body ng-app="app" ng-controller="MainCtrl"> <div ng-repeat="nameGroup in loopData"> & ...

Creating a customizable range input for pixel values: a step-by-step guide

I am looking to design a pixel range input. Here is an example: let slider = document.querySelector("input"); slider.addEventListener("change", () => { console.log(slider.value); }); <input type="range" min="5px" max="50px"> However, the r ...

Is the parent node of the input element the input element itself?

When working with a DOM object obj of type <input>, I encountered an issue where attempting to obtain its parent node using obj.parentNode resulted in the same obj being returned. Is this behavior specific to <input> objects? What other types o ...