How to to get elements value from html response at <script> </script>

So I get a response something like this:

<!DOCTYPE html>
<html>
<head>
--------------------
</head>
<body>
    <script>
        function onClickButton(word) {
        if (word == 'yes'){
          var authorizationCode = '360ad5ce-ecfe-4ad4-83d1-9254e89a3ccc';
          var state = 'c8271b81-4229-6a1f-bf9c-758f11c1f5b1';
        } else {
          alert(word);
        }
      }
    </script>
    <div class="shb-psua-sign-app-sandbox-container">
    --------------------
    </div>
</body>
</html>

How do I get var authorizationCode value? and save it as a variable in postman.
My code for now looks like this:

const $ = cheerio.load(pm.response.text())
var script = $("script").text();

pm.collectionVariables.set("access_token", script);

Now I get the whole script but how do I just get the value of the authorizationCode or even just using the gotten variable to get the value out of it?
I have also looked about different solutions for example:

const script = $('script').find("authorizationCode");  //but this doesn't work

Hi @akfas

Not the cleanest way but you could try this;

const response = pm.response.text();
//find a match with a regex
let authCode = response.match(/authorizationCode = '(.*?)';/g);
//remove first and last characters as needed with slice;
console.log(authCode[0].slice(21, -2));

1 Like

yeah, it works but I don’t know if the code is always that length

The length of code shouldn’t matter as the bits i’m removing are the start and end of the string…

So the slice 21 removes this;

authorizationCode = '

and the slice -2 removes this;

';

The bit you are left with is the code in between.

Like this;
image

1 Like

So if I mock up the example with extra stuff in the code it looks like this;

Hope this helps…

1 Like

yeah, sorry about that. I wasn’t fully concentrating when responding.

1 Like

no worries, glad it’s helped. :+1:

Could you help me guilde on how to get “Tekening naam” under the “Informatie” column and “ASN-AFS-B8822.dwg” under the “Data” column using Postman?
Thanks for your help!
I’m a new to automation API using Postman.

<div>
  <table cellspacing="0" rules="all" style="border-collapse:collapse;">
    <tr>
      <th scope="col">Informatie</th>
      <th scope="col">Data</th>
    </tr>
    <tr>
      <td>Tekening naam</td>
      <td>ASN-AFS-B8822.dwg</td>
    </tr>
  </table>
</div>

@maintenance-geologi5

This should probably be its own question.

I suspect that you need to convert the table to an JavaScript object which will then allow you to get to the elements you need.

However, I’m not sure how to do this in Postman and for once Google is not giving me the complete answer.

I can get to the table using the following code.

let html = `
    <div>
    <table cellspacing="0" rules="all" style="border-collapse:collapse;">
        <tr>
        <th scope="col">Informatie</th>
        <th scope="col">Data</th>
        </tr>
        <tr>
        <td>Tekening naam</td>
        <td>ASN-AFS-B8822.dwg</td>
        </tr>
    </table>
    </div>
`;

const $ = cheerio.load(html); // const $ = cheerio.load(pm.response.text())
let table = $.html("table");
console.log(table);

Then you would use a function similar to…

function tableToJson(table) { 
    var data = [];
    for (var i = 1; i < table.rows.length; i++) { 
        var tableRow = table.rows[i]; 
        var rowData = []; 
        for (var j = 0; j < tableRow.cells.length; j++) { 
            rowData.push(tableRow.cells[j].innerHTML);
        } 
        data.push(rowData); 
    } 
    return data; 
}

But the problem is that the table is being treated as a string.

So all of the functions I’ve tried for converting a table to an object are failing at the first hurdle (retrieving the number of rows) as its being treated as a string, not a table. Not sure if this is just because of how Postman is rendering this.