XPath picks a child node at random

I am testing a web application using the Selenium IDE and would like us to present some randomness for distributing our tests. I am currently using Selenium storeAttributeValue, where you give it an XPath expression, and it saves the first element that matches it (sorta). However, I do not want to keep the first match, I would like it to randomly select a child of the node.

eg.

//table[@id='mytable']//trselects all child trigrams of this table. //table[@id='mytable']//tr[0]selects the first tr (assuming now nested tables) //table[@id='mytable']//tr[3]selects the third tr, etc.

Is there any way (completely in xpath), I can say "Give me a random tr", i.e. //table[@id='mytable']//tr[SOMETHINGHERE]which every time i 'evalulate' / 'run' it will return one tr node which is in the set //table[@id='mytable']//tr.

+5
source share
3 answers

If the XPath expression will not change from one call to another, and the input source will also be the same, then you will need to provide variance with parameterization, otherwise with the same function with the same input, the same result will always be output (that's why I made a comment on declarative paradigm).

Sort of:

/table[@id='mytable']/tbody/tr[$pseudoRandom mod count(../tr) + 1]

If the source source has one estimate, the simplest pseudo-randomness in XPath will be

/table[@id='mytable']/tbody/tr[count(//node()|//@*) mod count(../tr) + 1]

, .

+4

bash, $RANDOM, :

/table[@id='mytable']/tbody/tr[floor('"${RANDOM}"' mod count(../tr)+1)]

xmllint, node. , xmllint FILENAME.xml:

xmllint --format --recover --xpath '/table[@id='mytable']/tbody/tr[floor('"${RANDOM}"' mod count(../tr)+1)]' FILENAME.xml
+2

XPath :

/table[@id='mytable']/tbody/tr[position() = {yourRandom}]

{yourRandom}

"" XPath , PL XP .

, # string.Format() XPath.

If you use XSLT , the code that causes the conversion can provide as an external parameter a sequence of random numbers encoded in an XML fragment. Then, the XSLT code will use each consecutive random number for each XPath expression.

0
source

All Articles