What is the best way to disengage a loop of elements within an internship?

In my scenario, the DOM structure is as follows:

<table id="campaigns">
   <tr>
      <th>Name</th>
      <th>Status</th>
   </tr>
   <tr>
      <td>first data</td>
   </tr>
   <tr data-id="1">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
   </tr>
   <tr data-id="2">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
   </tr>
   <tr data-id="3">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT edit</td>
   </tr>
   <tr data-id="4">
      <td>another data</td>
   </tr>
   <tr data-id="5">
      <td>next data</td>
   </tr>
</table>

When working with this in intern, I use the following code snippet:

this.remote
     .findAllByXpath("//*[@id='campaigns']/tr"") // get all items from the list
        .then(function (options) {
            return Promise.all(options.map(function (option) {
                return option.getVisibleText()
                    .then(function (text) {
                        if (text.includes("intern test")) { 
                             option.click(); 
                        }
                         return text;
                    })
            }))
         })

My requirement is to click only on the text containing "intern test". However, a problem arises where after clicking the first element, the subsequent ones are also clicked causing stale elements.

How can I modify the code to stop the loop once the first matching element is found?

Any help would be greatly appreciated. Thank you!

Answer №1

You don't have to search through every element to locate the first element that has the text "intern test"

Give this XPath a try with findAllByXpath (I'm not familiar with Javascript - selenium, so please adjust the syntax if needed):

//table[@id='campaigns']/tbody/tr/td[contains(text(),'intern test')]/..

This will give you all the tr elements in the table with the id campaigns that contain the text intern test.

Utilize indexing, such as 0, 1, 2, to target the first, second, and third elements that have the text intern test

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

What is the point of utilizing angular.extend in this situation?

After inheriting a codebase, I stumbled upon the following code snippet (with some parameters simplified and anonymized): /** * @param {float} num1 * @param {string} str1 * @param {string} str2 * @param {boolean} flag & @return (object) */ sr ...

Using VueJS: Passing a variable with interpolation as a parameter

Is there a way to pass the index of the v-for loop as a parameter in my removeTask function? I'm looking for suggestions on how to achieve this. <ol class="list-group"> <li v-for="task in tasks" class="list-group-item"> ...

Clear all CSS styles applied to a targeted division

My website built on Wordpress links to several CSS files. One specific div has its own unique style that is separate from the main Wordpress styles. Unfortunately, the Wordpress CSS ends up interfering with my custom div's layout. Is there a way in HT ...

Issue arises when isomorphic-dompurify is used alongside dompurify in Next.js 13 causing compatibility problems

I am currently facing a compatibility problem involving isomorphic-dompurify and dompurify in my Next.js 13 project. It appears that both libraries are incompatible due to their dependencies on canvas, and I am struggling to find a suitable alternative. M ...

Adding JSON data to an array with a click - a step-by-step guide

As I dive into working with React and integrating API JSON data into my project, I've encountered a small hurdle. I've implemented a function that allows users to enter a search query, resulting in a list of devices associated with their input be ...

Having trouble executing a MongoDB query through Mongoose without using a REST API

Dealing with the Express router has been an uphill battle for me. While Mongoose models work seamlessly within routes, I've hit a roadblock when trying to utilize the models in other files without routes. Whenever I attempt to run the file containing ...

What methods can I employ to utilize preg_match with jQuery?

Is it possible to validate a phone number in jQuery using preg_match? I have tried the following code without success: if (!preg_match("/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/", phone.val() )) { phone.addClass("needsfille ...

Develop a CakePHP CRUD view using a JavaScript framework

Creating a CRUD view in Cake PHP is simple with the following command: bin/cake bake all users This command builds the users table for CRUD operations. Is there a JavaScript framework that offers similar simplicity? ...

What are the steps to start using the Intersection Observer API right away?

Utilizing the Intersection Observer API, I can accurately determine whether an element is within the viewport or not. Is there a way to utilize the Intersection Observer API to detect if an element is in the viewport without relying on a callback function ...

Mongoose encountering an issue with undefined properties (specifically 'prototype') and cannot read them

I am currently using Mongoose 7.0.1 on Next JS 13.2.2 and React 18.2.0. After tirelessly searching for a solution to my problem, I am still struggling with connecting to a MongoDB. Every time I try to import mongoose into my project for the connection, an ...

Having trouble accessing the card number, expiration date, and CVC in vue using Stripe

Although I am aware that Stripe securely stores all information and there is no need for me to store it on my end, I have a specific requirement. When a user begins typing in the input area, I want to capture and access the card number, expiry date, and ...

AngularJS allows you to dynamically disable a button based on a value in an array

I have an array containing letters from A to Z and I want to create a list of buttons using them. $scope.alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); I also have another array: $scope.uniqChar = ['a', ' ...

Which scraping tool should be used to extract web data: Scrapy, Selenium, or Mechanize

Looking to gather data from a website, I noticed it displays about 50 records in tabular form. To access more data, the user needs to click a button that triggers an ajax call to retrieve and display the next 50 records. While Selenium webdriver (Python) ...

What is the reason for Selenium's @FindBy method not functioning properly with name, xpath, and css selectors?

Having trouble using @FindBy() in my Java program with Selenium. The program consists of multiple lines and I'm looking to share tasks between classes, starting with identity verification. I've created a class that includes login credentials for ...

Error encountered when attempting to convert a JSON object to a String using JSON.stringify(), due to a cyclic object value

I have a JSON object with the following structure: obj { name: "abc" , entriesList : "list of entry obj" , propertiesList : "list of properties obj" }; In this structure, an entry is also another object entry { info : "data obj" , ...

The ajax method for loading an xml file results in displaying the undefined message

When attempting to fetch content from an xml file using ajax, the page initially displays "undefined". However, upon refreshing the page, the content loads successfully. Take a look at my code snippet below: $.ajax({ type: "GET", url: "xm ...

What is the purpose of using a hash in a WebSocket handshake?

When establishing a Websocket connection, the client initiates by connecting to a tcp socket on a server and then performs a handshake. In the client's handshake, there is a base64 encoded key (Sec-WebScoket-Key). The expected response from the serv ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...

Constantly positioning the text cursor over the Textbox

I am currently in the process of developing a webpage that will feature only one text box for displaying information based on the input data provided. Our strategy involves utilizing either a Barcode Scanner or Magnetic Swipe as well as a Touch Screen Moni ...

Issues encountered when attempting to append the array objects to HTML using $.getjson

Hello, I have a JSON data structure as shown below: [{ "menu": "File", }, { "menu": "File1", }] I have created jQuery code to dynamically add the response to my HTML page like this: $(document).ready(function () { $.getJSON('data.json&a ...