Tips on how to choose all elements that do not include a specific value or group of values in cypress

Here's my situation:

selector: ".list > div"

const velue = [6, 9, 21]

Each div contains a different value. I want to remove elements that contain a specific value, then select the first remaining element and click on it. It should look something like this:

cy.get(".list > div").not('contains', value).eq(0).click()

I attempted to solve this using not, but it only accepts selectors and not values. Any thoughts? I couldn't find a similar topic on stackoverflow.

Answer №1

The value must be integrated within the contains selector in the not command.

It is considered a pseudo-selector, therefore the format is :contains()

cy.get(".list > div")
  .not(`:contains(${value})`)   // embed value into string template
  .eq(0)
  .click()

Answer №2

Cypress includes some handy utility lodash functions, such as _.filter(), which allows you to use a callback function to filter out the values 6, 9, and 21.

Sample HTML

<ul class="list">
  <div>1</div>
  <div>2</div>
  <div>6</div>
  <div>3</div>
  <div>9</div>
  <div>5</div>
  <div>21</div>
</ul>

Test code

cy.get(".list > div")
  .then(($el) => {
    // Filter out elements containing '6', '9', and '21'
    // and return the filtered array to Cypress chain
    return Cypress._.filter($el, (el) => {
      const text = el.innerText;
      return text != "6" && text != "9" && text != "21";
    });
  })

Check out this working example.

Answer №3

To exclude the numbers "3" and "5," you can utilize the :not selector in your code snippet like so:

cy.get('div:not(:contains("3"), :contains("5"))')

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

Running into an issue while attempting to generate functions in nodejs

I'm currently working on a function to authenticate a URL for a fetch request. However, when I attempt to call this function within the app.post callback, my Node.js server throws an error: "TypeError: authenticateUrl(...) is not a function". Does Nod ...

Create shorter nicknames for lengthy reference names within the ng-repeat loop

Is it possible to assign an alias to a long reference name in ng-repeat? Currently, I have 2 complex objects where one acts as a grouped index for the other. Although the ng-repeat template code is functioning correctly, it's getting hard to read and ...

Storing Json data in a variable within Angular 2: a step-by-step guide

Within the params.value object, there are 3 arrays containing names that I need to extract and store in a variable. I attempted to use a ForEach loop for this purpose, but encountered an issue. Can you spot what's wrong? var roles = params.forEach(x ...

converting JSON values into an array of strings in JavaScript

Greetings to all! I am excited to be a part of this community as I embark on my programming journey. I haven't been able to find any information on this particular topic, so I've taken the initiative to start a new discussion. If anything seems o ...

Conceal a card once verified within a bootstrap modal upon successful AJAX completion

On my delete page, there are multiple posts with a delete button. When the delete button is clicked, a Bootstrap modal opens asking for confirmation "Are you sure you want to delete this post? YES : NO" If the YES button is clicked, the .click(function(e) ...

What is the method for incorporating locales into getStaticPaths in Next.js?

I am currently utilizing Strapi as a CMS and dealing with querying for slugs. My goal is to generate static pages using getStaticPaths and getStaticProps in Next.js. Since I'm working with multiple locales, I have to iterate through the locales to re ...

JS Nav Dots are not activating the Active Class

I have been utilizing a code snippet from this source to incorporate a vertical dot navigation feature into a single-page website. The navigation smoothly scrolls to different sections when a link is clicked, with an active highlight on the current section ...

Using Node.js and Angular to Access Google Spreadsheet JSON Data Across Different Origins

Seeking to extract JSON data from a public Google Spreadsheets page, my initial attempt involved an AJAX call which successfully retrieved the data but I struggled with saving it to my $scope. Switching to $http.get led me to encounter cross-origin reques ...

Encountered a issue during the installation of an NPM module

I am a beginner in the world of web development. I attempted to set up an npm module using the command npm install grunt-contrib-watch --save-dev, however, I encountered the following error: npm WARN npm npm does not support Node.js v0.10.37 npm WARN npm ...

Using a JavaScript variable within an AngularJS scope

Currently in the process of building a webpage with the MEAN stack, I utilized the HTML5/Javascript sessionStorage variable to store the user data for access across all pages. Now, the challenge is passing this data into angularJS through the controller w ...

Making Angular2 Templates More Efficient with Array.prototype.filter()

I have a variable named networkInterface that includes an array called services. My objective is to create a checkbox input that indicates whether a specific service_id exists within the services array of the networkInterface. An illustration of JSON `int ...

Use .load() to set an image as background in the div

There is a block of code that is supposed to display images in a div with the class "img", but it isn't functioning correctly. Can you provide some guidance on how to fix this issue? <div class="other"><a href="/index1.html">Link1</a&g ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

What is the best way to switch between components when clicking on them? (The component displayed should update to the most recently clicked one

I am facing a challenge in rendering different components based on the navbar text that is clicked. Each onclick event should trigger the display of a specific component, replacing the current one. I have 5 elements with onclick events and would like to re ...

Understanding how to parse an array of arrays in JavaScript can be a

Looking for a function that can extract a value from an array containing multiple arrays? Simply use getValueFromArray(array, [2, 4]) to get the 4th element of the 2d array within the main array. Check out the code snippet below: function getValueFromArr ...

Initiating a file download using HTML

When I try to initiate a download by specifying the filename, instead of downloading, it opens in a new tab. The code snippet below shows what I am currently using. Additionally, I am working on Chrome Browser. <!DOCTYPE html> <html> < ...

Prevent the Stop Function from being executed repeatedly while scrolling

I have implemented infinite scrolling in my react/redux app. As the user nears the bottom of the page, more contents are loaded dynamically. However, a challenge arises when the user scrolls too fast and triggers the function responsible for fetching cont ...

Checking the length of user input with JavaScript

To ensure that the user enters at least 4 letters in a text box and show an error if they don't, I need help with the following code. It is partially working but currently allows submission even after showing the error message. I want to prevent subm ...

TypeScript encountered an unexpected { token, causing a SyntaxError

Despite multiple attempts, I can't seem to successfully run my program using the command "node main.js" as it keeps showing the error message "SyntaxError: Unexpected token {" D:\Visual Studio Code Projects\ts-hello>node main.js D:&bsol ...

Utilize the angularJS filter to emphasize the search text within the search results

I have a search box that filters results displayed on the screen. I am using a filter called 'startWith' for this purpose. Now, I need to implement a feature where the search text is highlighted among the search results in angularJS. For example ...