Search code examples
htmlpostmancheerio

Extract a value from a HTML response with Cheerio using Postman


I'm trying to get a value from request, that returns a HTML response, using Postman. I'm using Cheerio inside the script section.

The response looks like this:

    <table class="etlsessions-table" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th class="someclass1">
                    <div>
                        <span>info1</span>
                    </div>
                </th>
                <th class="someclass2">
                    <div>
                        <span>info2</span>
                    </div>
                </th>
                <th class="someclass3">
                    <div>
                        <span>info3</span>
                    </div>
                </th>
                <th class="someclass2">
                    <div>
                        <span>info4</span>
                    </div>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr class="someclass5">
                <td class="someclass">
                    <nobr>info5</nobr>
                </td>
                <td class="someclass6">
                    <nobr>info6</nobr>
                </td>
                <td class="someclass3">info7</td>
                <td class="someclass7">
                    <a href="http://www.google.com">someurl1</a>
                </td>
            </tr>
       </tbody>
    </table>

How I can get the info6 value from the someclass6 class?


Solution

  • As Cheerio is built-in to the Postman sandbox environment, you can use it to get the value of the element.

    I'm not sure of your complete use-case but you could add something basic like this to the Tests script and print the value to the console:

    const $ = cheerio.load(pm.response.text()),
        elementValue = $('.someclass6 nobr').text();
    
    console.log(elementValue)