Home > Back-end >  Iterating over a HTML table with Selenium using node.js
Iterating over a HTML table with Selenium using node.js

Time:08-26

I am new to Selenium and am trying to iterate over a set of table rows to find the time (e.g 08:00) in the first cell and then populate the adjacent 4 cells.

I find the table using the following but it tells me that forEach is undefined.

        driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody/tr"))
        .then(function (rows) {
          forEach(rows=> {
            
          });       
        })

This is the table structure.

        <table  id="deliveries">
            <tbody>
                <tr>
                    <th colspan="5">Location: //dynamically added</th>
                </tr>

                <tr>
                    <td >
                        <font>08:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td >
                        <font>09:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td >
                        <font>10:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td >
                        <font>11:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td >
                        <font>12:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>

Any advice on how I can get to the cells adjacent to the 'time' cell?

CodePudding user response:

Full blown Xpaths like that are extremely finnicky/very rarely work. I know its the automated code firefox/edge/whatever sent you but the html that selenium sees is different and your xpaths are very dependent on HTML. A single change on the website would break them. You should avoid them at all costs

You need to familiarize yourself with more advanced xpath functionality.

In this case you are better off going with element name and then text contained.

"//font[contains(.,'08:00')]")

I have tested this on firefox and it works on a codepen. I tried yours and it didn't give me anything.

Look at this to test Xpath expressions for your favourite browser. How to validate XPath using Firefox Web Developer Plugin?

CodePudding user response:

Hi guys and thanks for both of your inputs.

Being new to Selenium I hadn't discovered the executeScript function but I think I am now at the point where I can achieve what I want. The following shows my solution for iterating over the table rows - from there it should be fairly straight forward (though I've not tried yet).

Thanks again for taking the time to respond.

        var IterateRows = function (rows) {
            //iterate and populate row cells - return true if successful or false if error condition
            return true
        }
        
        var el = driver.findElements(By.xpath("//table[1]/tbody/tr"))
        
        driver.executeScript(IterateRows, el).then(function (returnValue) {
            console.log("Return Value of IterateRows -> "   returnValue);
        }); 

  • Related