A guide on incorporating a Java loop with Selenium automation

// Searching and deleting process
driver.findElement(By.cssSelector("input[type='search']")).sendKeys("Diversification Rule Template"); 
driver.findElement(By.className("delete-template")).click(); 
Alert alert = driver.switchTo.alert(); 
Thread.sLeep(200); 
alert.accept(); 
String SuccessMessage = driver.findElement(By.className("toast")).getAttribute("class");
System.out.println("Toast Message: " n SuccessMessage);
Assert.assertEquals("toast toast-success", SuccessMessage);

// Repeat for Diversification Rule Template2
driver.findElement(By.cssSelector("input[type='search']")).clear();
Thread.sLeep(200);
driver.findElement(By.cssSelector("input(type='search']")).sendKeys("Diversification Rule Template2"); 
driver.findElement(By.className("delete-template")).click(); 
driver.switchTo().alert(); 
alert.accept(); 
String SuccessDeleteMessage = driver.findElement(By.className("toast")).getAttribute("class");
System.out.println("Toast Message: " SuccessDeleteMessage);
Assert.assertEquaLs("toast toast-success", SuccessDeleteMessage); 

// Another search and delete process
driver.findElement(By.cssSelector("input[type='search']")).clear();
Thread.steep(200);
driver.findElement(By.cssSelector("input[type='search']")).sendKeys("New Diversification Rule");
driver.findElement(By.className("delete.template")).click();
driver.switchTo().alert(); 
alert.accept(); 
String SuccessDeleteMessage2 = driver.findElement(By.className("toast")).getAttribute("class");
System.out.println("Toast Message: SuccessDeleteMessage2); 

I am facing an issue with elements not being attached to the page document while trying to implement loops for easier deletion of users from the system. Any assistance or guidance on overcoming this problem would be greatly appreciated.

Answer №1

The search criteria can be iterated over to prevent StaleElementReferenceException by moving the elements each time.

List<String> items = new ArrayList<>() {{
    add("Diversification Rule Template");
    add("Diversification Rule Template2");
    add("New Diversification Rule");
}};

for (String item: items) {
    WebElement searchField = driver.findElement(By.cssSelector("input[type='search']"));
    searchField.clear();
    searchField.sendKeys(item);
    driver.findElement(By.className("delete-template")).click(); 
    Alert alert = driver.switchTo.alert(); 
    Thread.sleep(200); 
    alert.accept(); 
    String successMessage = driver.findElement(By.className("toast")).getAttribute("class");
    System.out.println("Message of Toast Message is: " + successMessage);
    Assert.assertEquals("toast toast-success", successMessage);
}

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

Tips for filtering data in JSON results within a Java servlet

I need to filter records by gender that are in a JSON response. I have a method in my DAO class that returns JSON data, so I want to parse the contents to filter by gender type. String results = bedsBean.allBedsInJson(); System.out.println(results); Th ...

Determine the size of an SVG while taking into consideration any strokes applied

Is there a way to seamlessly position an SVG in the corner of a div, despite having a dynamically generated stroke? Calculating the distance to the outermost part of the border proves difficult when dealing with irregular shapes like stars. Here's my ...

"What is the best way to ensure that a random array value is printed accurately within an If/Else statement in my Text Adventure

I am currently developing a text-based adventure game where the output in the console log is determined by specific input. The goal is for a message to appear if a goblin inflicts enough attack damage to reduce the player's defense to below 0, stating ...

The mssql node is experiencing an issue where it is unable to accept work due to the pool being

Recently, I encountered an issue with my node js app that utilizes the npm mssql module. Despite purchasing a cloud Windows 2012 server, I faced an error when trying to execute a stored procedure. The error is thrown at ps.prepare("exec usp_Get_Cars @para ...

The ajax method is encountering an issue when trying to perform an action: It is unable to find the necessary anti-forgery form field "__RequestVerificationToken" required for the operation

I am encountering an issue with my ajax method that triggers the action method from the controller. When I run this method, I receive an error stating: The required anti-forgery form field "__RequestVerificationToken" is not present. However, upon inspecti ...

Can Hibernate classes be mapped to multiple sets in Json format?

Here is a piece of code that I have mapped into Hibernate: @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="hall_id") Set<Literature> literatures; @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="hall_id") Set<RecycleFacility> rec ...

Could there be any issues with the structure of my mongoose schema?

I've been stuck for 3 hours trying to solve this problem. I can't seem to retrieve any data from my document. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var accountSchema = mongoose.Schema({ username: String ...

Is it possible to create tags in Material UI Autocomplete using events other than just pressing the 'Enter' key?

In my current project, I am utilizing the freesolo Autocomplete feature. My specific requirement is to create tags when input text is followed by commas or spaces. Currently, tags are created on the Enter event by Autocomplete, but I am looking for a way t ...

Launch all external links in Browser (NWjs)

While attempting to create my own independent version of Google Chat using NWjs, I encountered some obstacles. When a link is opened in the NWjs window, it opens in another NWjs window instead of the default system browser, which is what I want. I attemp ...

What is the best way to schedule a Java Selenium script to run every five minutes even if the system is shut down, and then automatically update the results in a Google

After developing a program in Eclipse using Java and Selenium Webdriver, I have successfully created functionality to: Navigate to a website and verify the value of a specific number (such as stock prices) If the current number value differs from the ...

Retrieve the Response object when an ASP.NET Button is clicked

Within my ASP.NET Webform, I have a server-side Button with an Onclick event registered on it. <asp:Button ID="UploadButton" CssClass="btn add btn-primary" runat="server" Text="Upload File" OnClick="UploadBut ...

What is preventing me from accessing arguments.callee within this function in sloppy mode?

In attempting to retrieve the arguments.callee property in the following basic function, I encountered an error preventing me from doing so. function foo(a = 50, b) { console.log(arguments.callee); } foo(10, 50); Why is this issue occurring? It appe ...

AngularJS directive that allows for either a click action to be passed or a ui-router state change to

I have a button (an a tag) that is displayed in multiple locations on my website. It is labeled "View Demo", and sometimes it directs to a demo page using an ui-sref: <a class="btn btn-primary" ui-sref="hamburger-push" target="_blank"> View Demo ...

Exploring methods to deactivate specific dates within the angular material datepicker

Is it possible to prevent specific dates from being selected based on the current date? For example, if today is 8/1/16, I would like to disable the next 4 days (any number of days could be chosen), excluding weekends. So, if today is 8/1/16, I would want ...

Django Implementation of JavaScript Confirmation Dialogue

Currently working on a Django form that requires a confirm/cancel dialog upon submission. I've considered sending POST data from jQuery, but I'm curious if it's possible to integrate a JavaScript dialog as middleware instead? ...

Issues encountered when using .delay() in conjunction with slideUp()

Issue: The box is not delaying before sliding back up on mouse out. Description: Currently, when hovering over a div with the class ".boxset" called "#box", another div "#slidebox" appears. Upon moving the mouse away from these two divs, #slidebox slides ...

What is the most effective method for transferring items between arrays in JavaScript?

In my situation, I am dealing with two arrays - 'objects' and 'appliedObjects'. My goal is to find an elegant solution in Javascript and/or Angular for transferring objects from one array to another. Initially, my approach was as follo ...

Message for validating if entity is not null

In my application, I currently have two entities set up as shown below. Both the 'name' and 'ldap' fields are not nullable. @Column(name = "name", nullable = false, unique = true) @NotNull @NotBlank private String name; @Column(name = ...

Leveraging JSON for fetching distinct identifiers from a database

When a user hovers over a parent span containing an empty img tag, I use JSON and jQuery to retrieve the src of the image from the database. The issue arises when the retrieved src is applied to all looped span images on the same page, instead of each imag ...

What could be causing my JavaScript code to not function properly in an HTML document?

Hello, I am a beginner in the world of coding and currently delving into web development. Recently, I was following a tutorial on creating a hamburger menu but I seem to be encountering some issues with the JavaScript code. I have double-checked my Visual ...