Answered question

How to use document.Evaluate of Javascript to extract a value from an XPath and use it in a transaction?

I have a product listing page and from there I want to select a product randomly to navigate to a product details page.
I want to use XPath using Javascript to extract the href URL and then use that value as part of the next transaction for navigation.

I am getting an error:
ReferenceError: "document" is not defined. (Script=script;Line=15)

My code:
var products = document.evaluate('count(//div[@class="details"])', document, null,0, null ).numberValue;
var randomProduct = Math.floor(Math.random() * products) + 1;
var element = document.evaluate( "(//a[@class=\"name\"])[" + randomProduct + "]" ,document, null, 9, null ).singleNodeValue;
logger.debug(element.href);
var url = element.href;

context.variableManager.setValue("prodUrl",url);

I am very new to this. I know I am doing something wrong. Please advise.
Thanks.

Praveen Kumar T.
Praveen Kumar T.

Praveen Kumar T.

Level
0
3 / 100
points

You can't use "document.evaluate" since NeoLoad does not act as a real browser and you do not have access to the DOM of the page.

Can't you create a variable extractor using an XPATH method to randomly extract your product?

Open the Advanced panel of your request in the design section and from here you can create your variable extractor.

 

Did you find this useful ?

No (0)

Yes (0)

0% of users found this answer useful