1

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 class="deliveries" id="deliveries">
            <tbody>
                <tr>
                    <th colspan="5">Location: //dynamically added</th>
                </tr>

                <tr>
                    <td class="time">
                        <font>08:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="time">
                        <font>09:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="time">
                        <font>10:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="time">
                        <font>11:00</font>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="time">
                        <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?

2 Answers 2

0

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?

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

3 Comments

thanks for the response. I accept your comment about the messy Xpath, but the reason I did it this way for now is because there are 6 tables and all have the same id & class. I've advised the dev's of this but fixiing it is not a priority for them as it's currently working which means I'm stuck trying to work around this. I like your option of using the "//font...." selector but this picks up one on each of the tables which is not what I need unfortunately. It would work if I knew how to iterate through the rows and select the time on that row. Cheers.
Are you able to use get elements which returns a list of all the elements matching a selector and just "//font")? You can iterate over them to get the text each one has
Also look at this answer for relative xpaths. First time coming accross the concept but it's literally what you need stackoverflow.com/questions/73484878/…
0

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);
        }); 

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.