How to retrieve all the text inside an element using Protractor and Selenium

<div class="test"> <span>
        <div>CONTENT </div>
    </span>
    <div>
        <ul>
            <li> <span>More text</span>
EXAMPLE1</li>
            <li>EXAMPLE2</li>
        </ul>
    </div>
</div>

Is there a way to extract all the inner content from the div with test class? I'm looking for an array of strings like: ["CONTENT","More text","EXAMPLE1","EXAMPLE2"].

Answer №1

Instead of directly using webdriver methods, you can utilize the convenient abstraction provided by protractor with element.all() as a wrapper around findElements(). Utilizing the map() function can help in obtaining a promise that resolves into an array of texts:

var texts = element.all(by.xpath("//div[@class='test']//*")).map(function (elm) {
    return elm.getText();
});

expect(texts).toEqual(["TEXT", "Other text", "TEST1", "TEST2"]);

Answer №2

Here is a similar approach you could take:

 List<WebElement> elements = driver.findElements(By.xpath("//div[@class='test']"));
 for(WebElement el: elements){
    //display text if not empty
    String text = el.getText();
    if(!text.isEmpty){
      System.out.println("Output :"+text);
    }
 }

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

Angular is not receiving the response from the controller in Spring MVC

I am currently working on implementing a basic CRUD Operation using Spring Restful Web Services and AngularJS. I have set it up to load all the details when the page is loading, however, I am not receiving any response. Controller :- @RestController publ ...

External function does not support jQuery types

In my theme.js file, I currently have the following code: jQuery(function ($) { accordion($) }) const accordion = ($) => ... By placing the accordion function directly into the jQuery function, Typescript is able to assist with the installed jquery ...

Detecting the visibility of a select option using Selenium

I am attempting to choose an option from a select element based on its visibility status. Is there a method to check if options within selects are visible? While they have a visibility attribute in the CSS, Selenium does not offer a .isDisplayed option fo ...

Utilize AJAX to dynamically refresh the page without having to reload it, enabling the use of both POST and GET methods

In order to avoid reloading the page when refreshing, I am utilizing Ajax for this particular 3-sided content along with JavaScript. Here is the code: Content: <ul id="nav"> <li><a href="ajax_adat.php?id=8&epul=0">Data</a>< ...

Is it possible to make each letter on a page a different color using JavaScript? I've noticed that there is always an additional set of span tags before each opening tag on the page

Page hosted using Google Drive Within the JS code: var doc_part = false; //denotes if a character is not part of the visible document var page_body; //function to generate random rgb values function randomInt(max, min) { return Math.floor((Math.ran ...

The functionality of jQuery on dropdown list change seems to be malfunctioning in Codeigniter

As a novice in CodeIgniter, I've scoured various StackOverflow threads for a solution to my issue without any luck. Below is the view code causing trouble: <select name="select1" id="select1"> <option value="0">None</option> ...

Preventing the Sending of Origin Header in Angular 2

I am facing an issue in my Angular2 project where the HTTP service is automatically adding the 'Origin' header with a value to all of the requests. Is there a way to stop Angular2 from including this 'Origin' header in the HTTP calls? A ...

Troubleshooting AngularJS Get Function Failure

New to the world of AngularJS, I find myself faced with a challenge. My .NET MVC WebAPI Restful app is up and running on an IIS server. However, when I attempt to query the API using , the response I receive is: [{"Id":1,"Name":"Glenn Block","Created":"00 ...

Mastering the Art of Configuring Filters in Plupload

I am using plupload with a unique feature that allows me to select and upload files with extensions that are not defined in the settings. For instance, I can upload .rar or .txt files without explicitly defining them in the filters. $(".uploadDocs").cli ...

multiple server-side tables with toggle buttons

I'm struggling with a page that contains 3 tables using datatables. The issue is that datatables can't handle more than one table, and after searching for a solution, I found a customized SSP Datatables at this link: here However, I'm wonde ...

Unable to properly connect my CSS file to the header partial

I am struggling to include my CSS file in the header partial Here is the link I am using: <link rel="stylesheet" href="stylesheets/app.css"> This is what my directory structure looks like: project models node_modules public stylesh ...

How can you select a variable using Jquery?

$.get('sampleurl.com',function(result){ target = $(result #specificID).attr("href") // Can someone help me with this??? }) I fetch content from an external website using $.get and store it in the result variable, now I'm trying to figure ou ...

Obtain the identifier of a div within nested HTML components by utilizing jQuery

How do I retrieve the id of the first div with the class: post, which is "367", using jquery in the given HTML code: <div id="own-posts"> <span class="title">Me</span> <div class="posts_container"> <div class="post"& ...

Upon executing `$('div')`, an empty array is returned

My goal is to retrieve all the div classes on a page using jQuery $('div');. However, the page does not natively support jQuery, so I am loading it through external links until $ === jQuery evaluates to true. Even on Stack Overflow where jQuery ...

Adding a nested data structure in a Meteor application

In my mongo collection, I have a document named exam. // meteor:PRIMARY> db.exam.find() { "_id" : "RLvWTcsrbRXJeTqdB", "examschoolid" : "5FF2JRddZdtTHuwkx", "examsubjects" : [ { "subject" : "Z4eLrwGwqG4pw4HKX" }, ...

AngularJS Uncaught ReferenceError: variable is not

I am facing an issue where I cannot retrieve a value from a function. I am performing REST requests for testing purposes and when trying to get a date, it always returns undefined. An Illustration $http.get('app/components/home/controller/test_calen ...

The function this.listCatModel.push is not a valid operation

I have a dropdown that I want to populate using the following code: this.categoryService.GetListItem(this.GetAllcatListUrl).subscribe(data=>{ this.listCatModel=data, this.listCatModel.push({ id:0, name:'Main Category', parent ...

How can I determine the Windows service pack version directly from my web browser?

Is there a method to determine the installed service pack from the browser? It doesn't seem to be available in System.Web.HttpBrowserCapabilities in asp.net. I am looking for a solution to alert users about the necessity of updating to XP Service Pack ...

Should I wait for my state to be updated with new information before triggering the render function?

Can someone assist me with restructuring the code below to ensure that the information in the data array is displayed on the initial page load? async componentDidMount() { const { id } = this.props.match.params const soccerApi = axio ...

What is the reason for LinkedIn_scraper scraping the same LinkedIn profile on various URLs?

from linkedin_scraper import Person for line in f: #line is url try: person = Person(line, driver=browser, scrape=True, close_on_complete=False) print(person.name) print(person.company) ...