简体   繁体   English

使用正则表达式从jmeter中的HTML响应中提取值

[英]Extract value from HTML response in jmeter using regular expression

I have a problem to extract a value from an HTML response of HTTP request using jmeter. 我在使用jmeter从HTTP请求的HTML响应中提取值时遇到问题。

This source html code to extract from: 此源html代码可从以下代码中提取:

<input type="text" name="ifu" size="32" value="1600553" class="champ_texte">

I'm using the following regular expression: 我正在使用以下正则表达式:

name of reference = ifu 
regular expression = //input[@type="text"][@name="ifu"][@ size="32"][@value="1600553"][@class="champ_texte"]

There is any problem in my expression. 我的表情有任何问题。
NB: my html response is an response of an Action struts. 注意:我的html响应是Action Struts的响应。

  1. If you are using XPath Extractor to parse HTML response ensure that Use Tidy (tolerant parser) option is CHECKED. 如果使用XPath Extractor解析HTML响应, 确保选中“ 使用Tidy(容忍解析器)”选项。

  2. Your xpath query should return value you want to extract. 您的xpath查询应返回要提取的值。
    So to get eg 'value' of your 'input' you have to use query like: 因此,要获取“输入”的“值”,您必须使用类似以下的查询:
    //input[@type="text"][@name="ifu"][@class="champ_texte"]/@value
    Extracted value (if any) will be stored in jmeter variable pointed in 'Reference Name' field (${ifu} in your case). 提取的值(如果有)将存储在“参考名称”字段中指向的jmeter变量中(在您的情况下为$ {ifu})。

  3. You can first test your xpath query using any other tool - Firefox addons at least: 您可以先使用任何其他工具(至少是Firefox插件)测试xpath查询:

The regular expression could be 正则表达式可以是

input type=\"text\" name=\"ifu\" size=\"32\" value=\"(\\d+)\" class=\"champ_texte

In more details, 更详细地说,

String x  ="<input type=\"text\" name=\"ifu\" size=\"32\" value=\"1600553\" class=\"champ_texte\">";
Pattern p = Pattern.compile("input type=\"text\" name=\"ifu\" size=\"32\" value=\"(\\d+)\" class=\"champ_texte");
Matcher m = p.matcher(x);
if (m.find())
   System.out.println(m.group(1));

If what you want to extract is the value property it is way better to use Css/Jquery Extractor: 如果要提取的是value属性,最好使用Css / Jquery Extractor:

With config: 使用配置:

  • Css/Jquery expression : input[name=ifu] CSS / jQuery表达式:输入[名称= ifu]

  • Attrbute: value 属性:价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM