1

I want to iterate through rows and columns using Selenium Webdriver (I am using Protractor for non-angular web page). I get number of links for doing this using JAVA code which uses WebElement class. However, I need to do this using JavaScript code.

Thanks, Sakshi

3 Answers 3

1

The following worked:

browser.driver.findElement(by.id('table_id')).then(function(table){
    table.findElement(by.tagName('tbody')).then(function(tbody){
        tbody.findElements(by.tagName('tr')).then(function(rows){
            for(i=0; i<rows.length;i++)
            {
                rows[i].findElements(by.tagName('td')).then(function(cols){
                    expect(cols[2].getText()).toMatch('ok');
                });

            }

        });
    });
});

Thanks, Sakshi

Sign up to request clarification or add additional context in comments.

Comments

0
var testData = [
    ['row 0, cell 0', 'row 0, cell 1', 'row 0, cell 2'],
    ['row 1, cell 0', 'row 1, cell 1', 'row 1, cell 2'],
    ['row 2, cell 0', 'row 2, cell 1', 'row 2, cell 2'],
];

// Traverse rows
$$('tr').each(function(rowElm, r) {
    // Traverse cols
    rowElm.$$('td').each(function(cellElm, c) {
        // Workout cell
        expect(cellElm.getText()).toContain(testData[r][c]);
    });
});

2 Comments

How do I get this table in var testData? <table id='some_id'> <thead></thead> <tbody> <tr id='id_1'> <td class='some_class'> <span>cell1</span> </td> <td class='some_class'> <span>cell2</span> </td> <td class='some_class'> <span>cell3</span> </td> </tr> <tr id='id_2'> <td class='some_class'> <span>cell4</span> </td> <td class='some_class'> <span>cell5</span> </td> <td class='some_class'> <span>cell6</span> </td> </tr> </tbody> </table>
the method you mentioned is for a generic 2-D array. However, I need to know about HTML rows and columns.
0

The simplest way is to use xpath

browser.driver.findElement(by.xpath("//table[@id='table_id']/tbody//tr")).then(function(rows){
  //your code...
  for(i=0; i<rows.length;i++){
    rows[i].findElements(by.tagName('td')).then(function(cols){
       expect(cols[2].getText()).toMatch('ok');
    });
  }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.