How can I access a child element within an element using Protractor?

I'm struggling to access the child element of an ng-repeat element.

After searching for solutions, I came across suggestions that didn't work for me. One tip was to try something like this:

var parent = element(by.repeater(''));
var child = parent.element(by.....);

However, when I attempt to use the child line, I can't seem to find the element function on the parent element.

http://example.com

If you look at the provided screenshot, you'll see the structure of the page code I'm trying to test.

My goal is to retrieve the alt attribute of the avatar image and extract its value (which is the Username of the User).

One possible solution I thought of is using .getInnerHTML() on the ng-repeat row to get a string containing all the code. From there, I could locate the alt attribute and its value through string manipulation, but this approach seems too brute-force, and I believe there must be a better method.

In simple terms, what I aim to do is to fetch row 4 from the repeater and obtain the Username of the user in row 4. That's essentially my objective here.

Answer №1

Give this a shot:

let container = element(by.repeater('f in feed'));
let item = container.all(by.xpath('//img[@alt="Pundeep"]')).first()

(or)

let container = element(by.repeater('f in feed'));
let item = container.all(by.xpath('//img[@alt="Pundeep"]')).get(0)

Answer №2

To retrieve the desired element in protractor, you can make use of both element.all() and get() locators. Follow these steps to achieve this:

var selectedElement = element.all(by.repeater('parent_locator')).get(3); // Retrieves the 4th element in the repeater. Remember that indexing starts from 0.
selectedElement.getAttribute('alt').then(function(attribute){
    var value = attribute; // The variable 'value' now stores the alt text
});

I hope you find this explanation helpful.

Answer №3

When referring to Protractor element documentation, an example is provided for finding child elements by chaining element find calls:

// Chaining 2 element calls.
let child = element(by.css('.parent')).
    $('.child');
expect(child.getText()).toBe('Child text\n555-123-4567');

// Chaining 3 element calls.
let triple = element(by.css('.parent')).
    $('.child').
    element(by.binding('person.phone'));
expect(triple.getText()).toBe('555-123-4567');

// Alternatively, using the $() notation shortcut instead of element(by.css()):

// Chaining 2 element calls.
let child = $('.parent').$('.child');
expect(child.getText()).toBe('Child text\n555-123-4567');

// Chaining 3 element calls.
let triple = $('.parent').$('.child').
    element(by.binding('person.phone'));
expect(triple.getText()).toBe('555-123-4567'); 

https://www.protractortest.org/#/api?view=ElementFinder.prototype.$

Answer №4

This illustration provides guidance:

element(by.css('select.custom-select:nth-child(1) option[value="12"]').click();

The nth-child() selector enables you to target a specific child element. In this scenario, I utilized a plugin containing two selects with identical classes where I needed to interact with a particular option in the first select and another option in the second select.

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

Troubleshooting Issue with Ionic's UI Router

Recently, I created a basic Ionic app to gain a better understanding of UI router functionality. To my surprise, when I ran the app, nothing appeared on the screen. Additionally, the developer tools in Google Chrome did not show any errors or information. ...

Tips for creating a login page with AngularJS

For my dynamic web application, I am utilizing AngularJS to create a master page structure. In the index.html file, I have an ng-view where I can inject other pages while keeping the menu, header, and footer constant. index.html <body ng-app="mainApp" ...

What methods can I use to emphasize three distinct dates?

I'm facing difficulty highlighting three specific dates: "10-11-2020", "22-11-2020", and "07-11-2020" using React.js and Datepicker. Unfortunately, my attempts have not been successful so far. Here is the code snippet I am working with: import React, ...

Creating a new component when a click event occurs in React

Currently diving into the world of React while working on a project that involves mapbox-gl. I'm facing an issue where I can successfully log the coordinates and description to the console upon hover, but I can't seem to get the popup to display ...

Utilize a Chrome extension to emphasize text within a contenteditable element by underlining it

I am in the process of developing a Chrome extension that highlights specific words when a user types them. For example, if a user inputs "hello," I want to highlight or underline it. The challenge arises when dealing with content editable divs in platform ...

Is there a more efficient method for translating arrays between JavaScript and PHP?

Currently, I am in the process of developing a web page that has the capability to read, write, and modify data stored in a MySQL database. My approach involves utilizing PHP with CodeIgniter for handling queries while using JavaScript to dynamically updat ...

Is it possible to incorporate some scripting to enhance the efficiency of deploying my mongoose database?

I'm completely new to setting up servers and diving into back-end development. My technical language is limited, making it challenging to find solutions through online research. I apologize in advance for my lack of expertise in this area. Currently, ...

Mastering the integration of bootstrap datepicker - jquery plugin within AngularJS

Is it feasible to integrate this plugin into an AngularJS export table with filters, including the usage of ng-model and other functionalities? The plugin In particular, I am interested in utilizing the "range" option for selecting dates from/to. ...

Issue with activating a Modal through a button inside a table row on React

I'm currently working on two files: Modal.js and Users.js. Users.js features a table with an API get query linked to it, and in the last column of the table, there's a dropdown for each row that contains three buttons: View, Edit, and Delete. My ...

Warning in Closure compiler: [JSC_POSSIBLE_INEXISTENT_PROPERTY] The property TextEncoder may not be defined on this object

In my Node.js application, when using the TextEncoder class, I typically do the following: const TextEncoder = require("util").TextEncoder; Although the code functions correctly, I am receiving an undesired warning from the Closure compiler: [JS ...

Different ways to resize an image in various sizes without relying on PHP thumb

I have developed an admin panel for managing reservations of charter, yacht and other vehicles. I am looking for a solution to upload only one image per vehicle and resize it in multiple sizes without relying on the phpthumb library due to its slow loadi ...

When running locally, the Web API functions as expected. However, once published, it

After successfully creating a Web API that returns data using AngularJS and displaying it on a local web page, I encountered an issue when I published the API to my web host. The error message "SyntaxError: JSON.parse: unexpected end of data at line 1 colu ...

Struggling to extract feedback with Selenium in Python

I am looking to extract reviews from a specific webpage on Sephora's site: . When inspecting the reviews, I found this example syntax: <div class="css-7rv8g1 " data-comp="Ellipsis Box ">So good! This primer smooths my skin and blurs my pores so ...

The clone() function in jQuery causes the properties of the original element to become distorted

Seeking help with a curious issue I'm encountering. After selecting a radio button and cloning it, the original radio button becomes unchecked while the cloned one behaves as expected. Any insights into why this might be happening would be greatly app ...

General procedure: identifying the specific input element triggering the keypress event, without directly specifying the ID or other identifiers

I am currently working on a Jquery script that handles validation, and I need help selecting the element on which the keypress event is triggered without explicitly passing the ID #elementid as shown in the code snippet below: var element = **choose the ob ...

What causes the discrepancy in the output values of the sha1 algorithm when using the packages object-hash and crypto/hashlib

On my frontend, I have implemented a JavaScript function that compares two sha1 hashes generated by the object-hash library. This is used to determine if there are any changes in the input data, triggering a rerun of a processing pipeline. To interact wit ...

Loading a different webpage seamlessly without having to reload the current one

Here is a snippet of my code in okay.html: {% extends "sch/base.html" %} {% load staticfiles %} {% block content %} <div class="row" id="ada"> <form action="" method="post> {% csrf_token %} <div align="center" class="cont ...

organizing data using backbone marionette router

I am attempting to display the query string, but I am only receiving null as output. The query string I am using is http:://localhost/admin/brands?foo=bar and no matter what I try, queryString remains null. I even attempted /brands/?foo=bar with no succes ...

What is preventing me from getting this straightforward CSS transition to function properly?

I'm really struggling with CSS transitions and trying to figure out why my transition for the div class=text is not working as expected. const menuToggle = document.querySelector('.toggle') const showCase = document.querySelector('.s ...

Ways to stop a form input value from disappearing when clicking on other fields?

Upon loading the page, the form's campaignid input value is automatically filled. However, clicking on other fields causes it to reset to default. Is there a way to prevent this default behavior and preserve the input value of campaignid? I've ...