是否可以使用JS DOM元素方法在邮递员中浏览html文档

时间:2018-09-28 13:01:49

标签: javascript dom postman

以及如何在Postman沙箱中声明它?

我需要这样的东西:

var h1Text = window.document.querySelector("h1").innerHTML;
console.log(h1Text);

我还需要通过这种方式找到一些东西

我有很多标签,我需要找到确切的标签。 Shuld我使用循环方法来查找所需内容,或者有更好的方法来获取具有请求属性的元素数据?

1 个答案:

答案 0 :(得分:0)

假设您正在使用 Postman 应用程序,您希望通过 Cheerio 库加载响应正文,并使用 JavaScript 对其进行解析。

这是我过去使用的一个示例,用于快速解决我的一个问题:

const html = cheerio(responseBody);

var results = [];

html.find('div[class="AutoGrid-cell min-w-0"] > div').each(function (i, e)
{
    results.push({
        "Item": e.children[e.children.length-3].children[0].children[0].children[0]["data"],
        "Price": e.children[e.children.length-4].children[0].attribs["value"]
    })
});