Using Protractor to extract text from multiple paragraphs

How do I retrieve the values of all paragraphs (p) at once? Below is an example of how my inspect view appears:

"Testing sample one."

"Testing sample two."

And here is a snippet of my code to extract the value of id 'run':

browser.findElement(by.css('[id=run]')).getText()

This only extracts the first value, or I can modify it to get the second value of the id. My requirement is to fetch both values in one go, within the same line of code. Can you please provide guidance on this?

Answer №1

Without providing an example in HTML and more details about what you want to extract, I will do my best to assist.

Typically, it is recommended to use element.all(by.css()), also known as $$(), instead of browser.findElements, unless you have a specific reason for using findElements. Here are some details explaining the difference.

As mentioned in a previous comment, there is a method called findElements() (view API documentation for findElements() here), which returns an array of matching elements. However, you cannot directly apply getText() to this array since it requires a single element (view API documentation for getText() here). You would need to iterate through the array.

Given the limited context provided, here are a few ideas to consider:

var allP = new Array();
var allPString = null; //if you desire one long string
//using 'element.all()' instead of browser.findElements
var allPEl = $$('p#run'); //equivalent to element.all(by.css('p[id=run]')); //returns array of found elements
var allPElBrowser = browser.findElements(by.id('run')); //returns array of found elements
var i = allPEl.length();
var j = 0;

allPEl.each().getText().then(function(text){  //use getAttribute('value') if getText() doesn't work
    allP.push(text); //add text to array
    allPString += text+' '; //add text to string with a space
    j++; //increment counter
    if(i === j-1){continueTest()}; //invoke continuation at the end of last loop due to asynchronous nature of 'then()'
});

continueTest = function(){
    allP.toString() //convert array to comma-separated list
    // include the remainder of your test case logic here
};

Keep in mind that I assume you require resolved promises for the content of the <p> tags to proceed further.

If you only need the array of <p> objects, which you can resolve later within an expect() statement, then simply use $$('p#id');.

If this solution does not meet your needs, please inform me of any missing components or difficulties you encounter.

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 best way to fetch information from an API using Angular5 with Material2 components?

Here are the 'table.component.html' and 'table.component.ts' files where I am pulling data from an API to display in a table. Below is the object received from the API: [ {position: 1, name: 'Hydrogen', weight: 1.0079, sym ...

I am unable to pass the req.params.id value as an input to a function located in a separate file

In my project, I've developed a REST API for user and coupon management. The main file driving this API is called coupon-api.js. This file contains the route definitions, while the functions to handle these routes are separated into two distinct files ...

Combine large arrays

Hey, I'm encountering some issues while trying to merge large arrays in a React Native app. Specifically, I am attempting to implement infinite scroll on React Native. The issue arises when the array reaches 500+ items; every time I add more items, my ...

Failure to Fetch the Uploaded File's Value Using the Parameter

Objective: My aim is to automatically upload the second input named "file2" to the action result using jQuery code that is compatible with the latest versions of Firefox, Chrome, and Internet Explorer. Issue: The problem arises when HttpPostedFileBase ...

How can I create a real-time page update using node.js?

I am completely new to node.js, but my main goal in learning this language is to achieve a specific task. I want to create a webpage where the content within a designated "div" can be swapped dynamically for users currently viewing the page. For example, ...

Conflict arising from duplicated directive names in AngularJS

Hey there, I've got a question for the experts. How can I prevent conflicts with directive names when using external modules? Right now, I'm utilizing the angular bootstrap module, but I also downloaded another module specifically for its carouse ...

Utilizing Data Filters to Embed HTML in Vue.js?

I have been attempting to utilize the Filter feature in Vue.js to insert HTML tags within a String. The documentation indicates that this should be achievable, however, I am not having any success. The objective is for the data to be just a String that is ...

The outcome of my function designed to calculate the highest possible profit using k transactions is a null array

I have developed a custom function to calculate the maximum profit from a series of stock transactions given a specific number of transactions allowed. Each transaction involves buying at a low price and selling at a higher price, with the rule that you ...

Retrieve information from the index resource using AJAX

I feel like I might be overcomplicating things, but basically, I'm trying to retrieve all the data from an index resource and have it load when a button is clicked using AJAX. I've been using a serializer to tidy up the JSON. Both "/categories" ...

Seeking assistance in configuring a dynamic payment amount feature on Stripe using Node.js

As a newcomer to node and javascript, I'm seeking guidance on how to proceed with the code below. I have copied it from the Stripe documentation, but I am unsure about the function for the commented token. Initially, everything was working fine with t ...

Can someone explain how to replicate the jQuery .css function using native JavaScript?

When referencing an external CSS file, the code may look something like this: #externalClass { font-family: arial; } Within the HTML file, you would use it like so: <a href="#" id="externalClass">Link</a> In the JavaScript file, you can ret ...

Executing a C# method from within a .js file using a Javascript function in a .cs file

I've made some updates based on the responses provided. My main area of confusion lies in the 'data' parameter and how to handle it in the JavaScript call. C# method [HttpPost] public string GetPreviewURL(string activityID) ...

the process of evaluating a pessimistic test scenario in selenium webdriver using c#

try{ IList<IWebElement> DropdownMenus = driver.FindElements(By.XPath("//*[@id='table-archive']/table[2]/tbody/tr/td[2]")); // Console.Write("List of TTMCs are: "); foreach (IWebElement ele1 ...

Issues with Selenium getting stuck while using pyvirtualdisplay

Running Selenium with Python on a server requires the ability to hide the Chrome display. Most of the time, the Python script runs smoothly, but occasionally it gets stuck when creating a new chromedriver session. It's puzzling why this happens interm ...

The utilization of ES Modules within a Next.js server.js

After reviewing a few examples in the Next.js repository, I came across: https://github.com/zeit/next.js/tree/master/examples/custom-server-express https://github.com/zeit/next.js/tree/master/examples/custom-server-koa I observed that these examples ut ...

Error: Selenium unable to locate Firefox user profile

Recently, I embarked on my journey of learning Selenium on a Linux server. My first step was to open two Putty terminals and navigate to the directory where my files are located. In terminal 1, to start the server, I executed the following command: DISPL ...

Issue with moveByOffset() function in Selenium

Encountering an issue with the moveByOffset() method. Having trouble moving a slider back to the left side after moving it to the right using this link. Could there be a problem with the moveByOffset() method or is there another method I should consider? ...

working with the express locals function

I've been trying to come up with a function that can access local variables, such as this one: // Retrieve user data by ID res.locals.findUser = function(user_id) { models.user.findOne({ '_id': user_id }, function(err, user) ...

The duration spent on a website using AJAX technology

We conducted an online survey and need to accurately calculate the time spent by participants. After using JavaScript and PHP, we found that the calculated time is not completely accurate. The original script was sending server requests every 5 seconds to ...

Encountering an issue with interacting with a radio button in Selenium using Java where an error is thrown

Today's challenge involves testing the functionality of the website pigu.lt by updating personal data. Take a look at the code snippet below: import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; i ...