Search code examples
javascriptpostmancheerio

get element value from HTML response using Postman


Somewhere in the HTML response there is an element:

<input name="__RequestVerificationToken" type="hidden" value="I_NEED_THIS" />

How can I get it's value using Postman?

This is my code but I'm not happy with what it returns

const $ = cheerio.load(pm.response.text());
console.log($("title").text()); // get title to check if it works
console.log($("__RequestVerificationToken").attr('value')); //returns undefined
console.log($("__RequestVerificationToken").text()); //returns null

Solution

  • "__RequestVerificationToken" is not a valid selector since it is a name attribute you're looking for.

    Instead, use the valid CSS selector like this:

    const $ = cheerio.load(pm.response.text()).;
    console.log($("input[name='__RequestVerificationToken']").attr('value'));
    console.log($("input[name='__RequestVerificationToken']").text());