XPath 随机选择一个子节点 [英] XPath select a child node at random

查看:37
本文介绍了XPath 随机选择一个子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium IDE 进行一些 Web 应用程序测试,并且想引入一些随机性来展开我们的测试.我目前正在使用 Selenium 的 storeAttributeValue,在那里你给它一个 XPath 表达式,它存储匹配它的第一个元素(sorta).但是我不想存储第一个匹配项,我希望它随机选择一个子节点.

例如

//table[@id='mytable']//tr 选择此表的所有 tr 子项.//table[@id='mytable']//tr[0] 选择第一个 tr(假设现在是嵌套表)//table[@id='mytable']//tr[3] 选择第三个tr,依此类推

有什么方法(完全在 xpath 中)我可以说给我一个随机的 tr",即 //table[@id='mytable']//tr[SOMETHINGHERE] 每次我评估"/运行"它将返回"一个位于 //table[@id='mytable']//tr 集合中的 tr 节点.

解决方案

如果 XPath 表达式不会从一个调用更改为另一个调用,并且输入源也将相同,那么您将需要通过参数化提供方差,否则具有相同输入的相同函数将始终输出相同结果(这就是我对声明式范式进行评论的原因).>

类似于:

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

如果每个输入源有一个评估,XPath 中最简单的伪随机性是

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

换句话说,用整个输入源的某些属性作为种子进行一些伪随机计算.

I'm doing some web app testing using Selenium IDE, and would like to like to introduce some randomness to spread out our tests. I'm currently using Selenium's storeAttributeValue, where you give it a XPath expression, and it stores the first element that matches it (sorta). However I don't want to store the first match, I'd like for it to randomly select a child node.

e.g.

//table[@id='mytable']//tr selects all the tr childs 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 some way (entirely in xpath) I can say "Give me a random tr", i.e. //table[@id='mytable']//tr[SOMETHINGHERE] which everytime I 'evalulate'/'run' it will 'return' one tr node that is in the set of //table[@id='mytable']//tr.

解决方案

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.

这篇关于XPath 随机选择一个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆