how to get an attribute value from a href link in Jmeter

1.7k Views Asked by At

I have a list of links in a table. One link from table is shown below:

<a href="/hix/entity/assisteradmin/viewassisterinformation?assisterId=CSyTid9fDmtJ4OqoMA3mvA">
   A1 Counselor
</a>

in an HTML response in JMeter.

I am trying to get the value of assisterId (CSyTid9fDmtJ4OqoMA3mvA) from the href link text from the HTML response through JMeter.

Can someone please help me through this?

3

There are 3 best solutions below

0
On

I'm not familiar with JMeter, but in xpath point of view, you can try using substring-after() function to extract text that occurs after "assisterId=", something like this :

substring-after(//a/@href, 'assisterId=')

output in xpath tester, given the above HTML snippet as input :

String='CSyTid9fDmtJ4OqoMA3mvA'
0
On

You can get this value via Regular Expression Extractor as:

  1. Add Regular Expression Extractor as a child of the request which returns your data snippet
  2. Configure it as follows:

    • Reference name: anything meaningful, i.e. assisterId
    • Regular Expression: <a href="/hix/entity/assisteradmin/viewassisterinformation\?assisterId=(.+?)">
    • Template: $1$
  3. You will be able to access extracted value as ${assisterId} where required

See USING REGULAR EXPRESSION EXTRACTOR guide for more details on correlation in JMeter using regular expressions.

0
On

Following the answer of har07, you could use a XPath extractor as a child of the HTTP Request sampler from which you get the list of links.

The major drawback of the XPath extractor is that it won't work if Tidy errors are returned.

Consider using a Regular Expression extractor instead, as it would work at all time if your regular expression match the HTML response returned by the server.

In your case the regex and the other Regex extractor parameters would be like :

  • Reference Name (the variable in which the regex match is stored) : ASSISTER_ID
  • Regular Expression : <a href=".*?viewassisterinformation\?assisterId=(.*?)">
  • Template (the capturing group of the regex to store in ASSISTER_ID ) : $1$
  • Match No. : 1
  • Default Value (if there is no match) : REGEX_FAILED

In case you want to process all the links from the list, set the Match No. to -1 and use it with a ForEach Controller.