XPath select a child node at random XPath select a child node at random selenium selenium

XPath select a child node at random


If the XPath expression isn't going to change from one invocation to another, and the input source is also going to be the same, then you will need to provide the variance by parameterization, otherwise the same function with the same input will always output the same result (that's why I've make the comment on declarative paradigm).

Something like:

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

If there is going to be one evaluation per input source, the most simple pseudo randomness in XPath would be

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

In other words, making some pseudo randomness calculation with some property of the whole input source as seed.


If you are using bash commandline, you can get a pseudorandom number from the $RANDOM variable like such:

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

This was the only way I could get a random number in while using xmllint to try and get a random child node. Here is the command using xmllint and some FILENAME.xml:

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


Construct dynamically the XPath expression and then evaluate it:

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

where

{yourRandom}

must be substituted in the "skeleton" of the XPath expression with a random number obtained in the PL hosting XPath.

For example in C# one would use the string.Format() method to construct the XPath expression.

If you are using XSLT, the code that invokes the transformation may provide as an external parameter a sequence of random numbers encoded in an XML fragment. Then the XSLT code would use each consecutive random number for each XPath expression.