Quantcast
Channel: [Q&A - FAQ Neotys] Last questions
Viewing all articles
Browse latest Browse all 2283

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 2283

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>