SEARCH
Showing results for 
Search instead for 
Do you mean 
Reply
Occasional Contributor
Posts: 3
Registered: ‎06-18-2017
Accepted Solution

HTML Extraction Script

[ Edited ]

Im having touble extracting text from an html page using XPath. This is just an arbitrary example of a bigger problem I'm trying to solve. 

 

In the attached HTML I'm trying to extract the text 600396 from.

     <td id="a"><a href="/Home/Contact">600396</a></td>

 

My script shoud print the extracted value to the event long and set a it to a custom property. When I test my XPath it seems to be working but when I run the script it only returns an "H" which I though was strange, it turns out its returning the entire html document in a string array. If I remove the Index selector from my the query [0], getResponse() just returns the entire html document. 

 

 

-- Script
var msg = $context.currentItem.previousItem.children[0]
var query = msg.getResponse(msg.HTTP_BODY_AS_HTMLL, '//*[@id="a"]/a')[0];


$context.result.postMessage($context.result.LEVEL_INFO, query);
$prop.set("MessageClip", "clipProp", query);

 

I'm brand new with this, any help is appriciated.

 

 

 

 

SOASTA Employee
Posts: 1,499
Registered: ‎04-30-2012

Re: HTML Extraction Script

Hi rasedgwick,

You should try to be more specific with your XPaths though I know your example is generic in an effort to understand what's amiss.  Using index references within an XPath makes them fragile and subject to invalidation if anything changes.

 

For your example you would use something like //*[contains(@href,'Contact')]/text()  Typically there is a class and name that will distinguish the XPath and you can use something like //*[@class='specific name']/text() or //*[@class='specific name']/@href

 

This can all be done in a property extraction and it looks like you're trying to do it via JS instead.  Doing it via a property will simplify the process dramatically unless there is a specific need for a JS in this instance.

Regards,

Cory
Director of Performance Engineering - East
Have You Seen The Tutorial Videos? Click Here
Occasional Contributor
Posts: 3
Registered: ‎06-18-2017

Re: HTML Extraction Script

Thanks!

 

 

SOASTA Employee
Posts: 1,499
Registered: ‎04-30-2012

Re: HTML Extraction Script

Not a problem.  Let us know if you run into anything else and feel free to check out the videos on how to do a number of different things within CloudTest.  The link is in my signature below.

Regards,

Cory
Director of Performance Engineering - East
Have You Seen The Tutorial Videos? Click Here