user9847788
user9847788

Reputation: 2445

How to read a specific row in a table in Protractor without using XPath?

I am trying to write a Protractor test that checks the number inside the <strong> tag below:

enter image description here

I managed to get it working for 1 <tr> using nth-child below:

element(by.css('td:nth-child(4)')).getText().then((text) => {
    console.log('Text', text);
});

But if I have multiple rows within the table, it only prints the 1st row.

Can someone please tell me what changes I need to make so that I look through each row within the table & print the 4th child?

Upvotes: 0

Views: 270

Answers (1)

yong
yong

Reputation: 13712

Use element.all()

element.all(by.css('tr > td:nth-child(4)')).getText().then((texts) => {
    // texts is a string array, includes the 4th column values
    console.log('Text', texts);
});

// or use element.all().each() 
element.all(by.css('tr > td:nth-child(4)')).each((it)=>{
  it.getText().then((text)=>{
    console.log('Text', texts);
  })
});

Upvotes: 1

Related Questions