Is there a way to retrieve a random node from an Xpath that contains multiple nodes whose count varies weekly? I am looking for a solution that would work in one of two ways:
- Return the total number of nodes corresponding to the Xpath, allowing me to then randomize the selection.
- Alternatively, provide a method to randomly select 1 out of 25 Xpaths.
I have attempted various approaches to solve this issue without success. One potential solution I found involves:
productCounter(){
let c = document.evaluate('count(//*[@itemtype]/*[@data-order-bacu])', document, null, XPathResult.ANY_TYPE, null);
console.log(c.numberValue);
}
This script should give me the count of nodes within the Xpath for randomizing purposes. However, when using webdriverIO, it throws an error:
document is not defined
As an alternative, I decided to try using the "browser.elements" function from webdriverIO. But I am uncertain if this approach will be helpful.
productCounter(){
let a = browser.selectByIndex("//*[@itemtype]/*[@data-order-bacu]");
console.log (a);
}
The console.log output looks similar to the following. Could this information be used to determine the number of nodes as well?
{ sessionId: 'a45061ba0d9dfcf0b241877bb2951cc8', value: [ { ELEMENT: '0.5928327282420562-1', 'element-6066-11e4-a52e-4f735466cecf': '0.5928327282420562-1', selector: '//*[@itemtype]/*[@data-order-bacu]', value: [Object], index: 0 }, { ELEMENT: '0.5928327282420562-2', 'element-6066-11e4-a52e-4f735466cecf': '0.5928327282420562-2', selector: '//*[@itemtype]/*[@data-order-bacu]', value: [Object], index: 1 }, ... { ELEMENT: '0.5928327282420562-8', 'element-6066-11e4-a52e-4f735466cecf': '0.5928327282420562-8', selector: '//*[@itemtype]/*[@data-order-bacu]', value: [Object], index: 7 }, { ELEMENT: '0.5928327282420562-9', 'element-6066-11e4-a52e-4f735466cecf': '0.5928327282420562-9', selector: '//*[@itemtype]/*[@data-order-bacu]', value: [Object], index: 8 } ], selector: '//*[@itemtype]/*[@data-order-bacu]',