Selenium 从 xpath 获取动态 id
Selenium RC 有没有办法从 xpath 获取 id?
如果我有 xpath,
/html/body/div/div//input
我想获取与 xpath 关联的所有节点的 id
Is there a way in Selenium RC to get the id from the xpath?
If I have the xpath
/html/body/div/div//input
I want to get the id of all the nodes associated to the xpath
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
getAttribute
与getXpathCount
结合使用。Java 中的 Selenium 1 示例如下:
Java 中的 Selenium 2 示例如下:
You can use
getAttribute
in combination withgetXpathCount
.A Selenium 1 example in Java would be:
A Selenium 2 example in Java would be:
您可以通过使用
this.browserbot.findElement('/html/body/div/div//input')
运行 javascript 来获得:当然,这取决于源语言,但它会是这样的(在perl中,未经测试):
我一直认为应该有一种更直接的方法来做到这一点,但我还没有找到它!
EDITED 根据注释,for 循环计数应从 1 开始并包含
$count
,而且 findElement 行在输入前只需要一个正斜杠。EDIT2 根据进一步的评论添加完全不同的想法:
附加到每个页面的 Selenium javascript 包括一个名为
eval_xpath
的函数,该函数返回给定查询的 DOM 元素数组。听起来像你想要的吗?我认为 JavaScript 应该是这样的(同样,未经测试):
You can get that by running a javascript, using
this.browserbot.findElement('/html/body/div/div//input')
:Of course, this depends on the source language, but it would be something like this (in perl, untested):
I always thought there should be a more direct way to do this, but I haven't found it yet!
EDITED based on the comments, the for loop count should start from 1 and include
$count
, also the findElement line only needs one forward-slash before input.EDIT2 Adding a completely different idea based on further comments:
Selenium's javascripts that get attached to every page include a function called
eval_xpath
that returns an array of DOM elements for a given query. Sounds like what you want?Here's what I think the javascript would look like (again, untested):