Extracting the value of a Json element using the specified Element ID

While delving into JavaScript and the JavaScript framework, I have another query. I am seeking clarity on how to access:

{ Element: 1 }, { Element: 2},... 

This contains detailed information about the array.

Furthermore, I aim to format it as JSON like so:

[{name: "MyName", surname: "mySurname"}, {name: "MyName", surname: "mySurname"}]

and so forth, to achieve this for any:

{ Element:1}, {Element:2}, ....

I am utilizing nightwatch.js and my current code appears as follows :

.elements('css selector', 'ul li', function(res){
    console.log(res.value)
    console.log(res.value[1].ELEMENT)
    browser.elementIdAttribute(res.value[1].ELEMENT, 'li', function(newRes) {
      console.log(newRes.value)
    })
  })

this results in


[ { ELEMENT: '1' },
  { ELEMENT: '2' },
  { ELEMENT: '3' },
  { ELEMENT: '4' } ]
2
null

Attached is the HTML:

<html>
<meta charset="utf-8">
<body>
<ul class="random">
    <li class="list">
        <div class="name">Nick</div>
        <div class="surname">Kyrgios</div>
        <div class="age">22</div>
        <div class="city">London</div>
    </li>

    <li class="list odd">
        <div class="name">Nick</div>
        <div class="surname">Kyrgios</div>
        <div class="age">22</div>
        <div class="city">London</div>
    </li>
    <li class="list">
        <div class="name">Nick</div>
        <div class="surname">Kyrgios</div>
        <div class="age">22</div>
        <div class="city">London</div>
    </li>

    <li class="list odd">
        <div class="name">Nick</div>
        <div class="surname">Kyrgios</div>
        <div class="age">22</div>
        <div class="city">London</div>
    </li>
</ul>
</body>
</html>

For reference, here is a link to the documentation

Answer №1

It seems that the goal you are aiming for is not feasible in this context.

The reason you are encountering null as a result is likely due to using an incorrect command. In order to properly assign values to elements, it is essential to refer to the correct command. You can find more information on this at this link.

To achieve your desired outcome, consider utilizing .elementIdText in the following manner:

module.exports = {
  'Test' : function(browser) {

    function iterate(elements) {
      elements.value.forEach(function(el) {
        browser.elementIdText(el.ELEMENT, function(result) {
          console.log(result.value);
        });
      });
    }

    browser
      .url('http://simsonivini.lv/nightwatch.php')
      .waitForElementVisible('body', 8000)
      .elements('css selector', 'ul li div', iterate)
      .end();
  }
};

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

Utilizing Scala Play framework for json serialization of parameterized types

Consider the scenario where I have a trait defined like this: trait MyTrait[T] { def myVal: T } And then, I create a case class that extends this trait: case class MyClass(val foo: Int, val myVal: Boolean) extends MyTrait[Boolean] Next, I define an ...

Unexpected behavior observed with negated character: ? ^

I am looking to create a form where users can input their phone number and have the flexibility to choose how they want to separate the numbers. I have been using a regex pattern for validation: var regex = /[^0-9 \/-\\\(\)\+ ...

Utilizing the map function in Angular while disregarding any null values

I have an array of objects in my dataset. Here's a glimpse of how it is structured: [ { "id": 1, "name": "john", "address": { "number": 42, "street": "High Street"} }, { ...

Mastering Meteor: Techniques for Manipulating Mongodb Data in Real Time Display

Within my Meteor application, I have accomplished the successful publication of data from the server and its subscription on the client side. Instead of directly displaying raw data on the client's screen, I am interested in performing some calculatio ...

Issue with submitting forms in modal using Bootstrap

In the model box below, I am using it for login. When I click on either button, the page just reloads itself. Upon checking in Firebug, I found something like this: localhost\index.php?submit=Login <div class="modal fade" id="loginModal" tabindex= ...

Guide for verifying the minimum and maximum values when selecting a particular product from a dropdown menu

My goal is to implement validation for the width textbox when a user selects a specific product from the dropdown menu. The validation should only allow the user to enter a value between 10 and 200. I have successfully retrieved the selected value, but I ...

Tips for resolving CORS problems when trying to pull data from an API by utilizing jQuery AJAX and a JAVA backend

Currently, I am working on developing a front-end application to display data fetched from an API. This particular API was created using JAVA and Swagger.io by an android engineer. At the moment, the API does not have any authentication mechanism in place, ...

Is JSON parsing in Scala more challenging in Play 2.1 compared to Jerkson?

Using Jerkson, I successfully parsed a String containing a JSON array with the following code: com.codahale.jerkson.Json.parse[Array[Credentials]](contents) The contents of the String were as follows: [{"awsAccountName":"mslinn","accessKey":"blahblah"," ...

What is the reason for using a callback as a condition in the ternary operator for the Material UI Dialog component?

I am in the process of reconstructing the Material UI Customized Dialog component based on the instructions provided in this particular section of the documentation. However, I am unable to grasp the purpose behind using a callback function onClose conditi ...

Is the xmlhttprequest timeout/abort feature not functioning as anticipated?

Check out this snippet of my AJAX function: /** * This function initiates an AJAX request * * @param url The URL to call (located in the /ajax/ directory) * @param data The data to send (will be serialized with JSON) * @param callback The fu ...

Sending File from React to Express Causes 404 Error

My current project setup involves a React application housed in a client folder and an Express server located in the root directory. Within the React app, there are functionalities for selecting files and submitting them. I aim to transfer these files from ...

Is there a way to block the .load() function directly from the browser console?

I am looking to enhance the user experience on my website by dynamically loading certain content after login. This involves using a $.post(...) method to interact with a servlet that verifies the user's credentials, followed by a $.load(url) function ...

Utilize Autodesk Forge to adjust the DataVisualisation sprite offset and customize various THREE js sprite parameters

After going through this tutorial at , I successfully implemented a system in my Forge viewer app to add sprites on mouse click. However, I am curious if there are other parameters that can be modified besides changing spriteSize, such as the sprite ancho ...

Spinning a 3-dimensional vector in THREE.js using dual axes

Apologies for potentially posting a duplicate question. I am working with two 3D vectors: Center: (0, 0, 0) Orb: (0, 0, 100) My goal is to rotate the orb-vector around the center-vector on both the X and Y axes to ensure an object is always in view of ...

How to dynamically modify a list box with JavaScript

I have an index.html file with a select tag containing multiple options for a drop-down: <label for="edu">Education</label> <select id="edu" name="edu"> <option value="0">10th</option&g ...

Access a particular html tag as a value within an object

Recently, I've been working on AngularJS version 1.6 and am faced with an API containing multiple objects structured similar to the example below: { "description": " <p><a href=\"url">link</a>text</p><p><a href ...

Guide to setting up a horizontal button menu and dropdown submenu beneath the navigation bar using Bootstrap 4

How can I design a horizontal button menu with submenus below the navigation bar in Bootstrap 4? Take a look at the image below to see what I'm aiming for: https://i.sstatic.net/j6b8a.png https://i.sstatic.net/rmtrM.png If you have any ideas or su ...

Transform JSON data into an array of arrays

For my project, I am using the python SimpleHTTPWebserver to serve up various files, including a valid JSON file named "file.json". In my javascript front end, I need to interpret this JSON object as an array of arrays. For example: { "val1": 101, "va ...

Obtain the Index of a Two-Dimensional Array in Python

When processing geographic raster data, I rely on the osgeo library from the gdal module. #Determine the size of the input raster (cols, rows) cols = ds.RasterXSize #3531 rows = ds.RasterYSize #3314 To read all the data, I create an array: data = band.R ...

How can one transform a json object into a json string and leverage its functionalities?

I recently encountered an issue with a JSON object that contains a function: var thread = { title: "my title", delete: function() { alert("deleted"); } }; thread.delete(); // alerted "deleted" thread_json = JSON.encode(thread); // co ...