首页 > 解决方案 > 获取隐藏表单元素的属性值

问题描述

我正在使用 Postman 来自动化一些测试。我需要在名为“执行”的隐藏字段上获取 value 属性的值:

<form class="app-form" method="post" id="fm1" action="login" _lpchecked="1">
    <input type="hidden" name="execution" value="633ffc0f">
</form>

在邮递员中,只有 Cheerio 可用于此。我尝试了以下变体,但没有一个有效:

$('input#execution').attr("value");
$('input[name=execution]').attr("value");
$('input[type=hidden]').attr("value");
$(':hidden#execution').attr("value");
$('input:hidden[name=execution]').attr("value");

非常感谢!

标签: javascripthtmlpostmancheerio

解决方案


使用您的示例 HTML 片段,您可以在选项卡中使用此基本代码Tests将该值保存到 Postman 中的环境变量中:

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

pm.environment.set("hiddenValue", $('input[name="execution"]').val());

邮差


推荐阅读