Selenium 从 xpath 获取动态 id

发布于 2024-09-04 22:57:24 字数 142 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-09-11 22:57:25

您可以将 getAttributegetXpathCount 结合使用。

Java 中的 Selenium 1 示例如下:

int inputs = selenium.getXpathCount("/html/body/div/div/descendant::input").intValue();
for (int i=1; i<=inputs; i++) {
    System.out.println(selenium.getAttribute("/html/body/div/div/descendant::input[" + i + "]@id"));
}

Java 中的 Selenium 2 示例如下:

List<WebElement> inputs = driver.findElements(By.xpath("/html/body/div/div/descendant::input"));
for (WebElement input : inputs) {
    System.out.println(input.getAttribute("id"));
}

You can use getAttribute in combination with getXpathCount.

A Selenium 1 example in Java would be:

int inputs = selenium.getXpathCount("/html/body/div/div/descendant::input").intValue();
for (int i=1; i<=inputs; i++) {
    System.out.println(selenium.getAttribute("/html/body/div/div/descendant::input[" + i + "]@id"));
}

A Selenium 2 example in Java would be:

List<WebElement> inputs = driver.findElements(By.xpath("/html/body/div/div/descendant::input"));
for (WebElement input : inputs) {
    System.out.println(input.getAttribute("id"));
}
素染倾城色 2024-09-11 22:57:25

您可以通过使用 this.browserbot.findElement('/html/body/div/div//input') 运行 javascript 来获得:

当然,这取决于源语言,但它会是这样的(在perl中,未经测试):

#first count the number of inputs with ids
my $count = $selObj->get_xpath_count('/html/body/div/div//input[@id]');

#build a javascript that iterates through the inputs and saves their IDs
my $javascript;
$javascript .= 'var elements = [];';
$javascript .= "for (i=1;i<=$count;i++)";
$javascript .= "  elements.push(this.browserbot.findElement('/html/body/div/div/input['+i+']').id);";
#the last thing it should do is output a string, which Selenium will return to you
$javascript .= "elements.join(',');";

my $idString = $selObj->get_eval($javascript);

我一直认为应该有一种更直接的方法来做到这一点,但我还没有找到它!

EDITED 根据注释,for 循环计数应从 1 开始并包含 $count,而且 findElement 行在输入前只需要一个正斜杠。

EDIT2 根据进一步的评论添加完全不同的想法:

附加到每个页面的 Selenium javascript 包括一个名为 eval_xpath 的函数,该函数返回给定查询的 DOM 元素数组。听起来像你想要的吗?

我认为 JavaScript 应该是这样的(同样,未经测试):

var elements = eval_xpath('/html/body/div/div//input',this.browserbot.getCurrentWindow().document);
var results = [];
for (i=0;i<elements.length;i++){
  results.push(elements[i].id);
}

results.join(',');

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):

#first count the number of inputs with ids
my $count = $selObj->get_xpath_count('/html/body/div/div//input[@id]');

#build a javascript that iterates through the inputs and saves their IDs
my $javascript;
$javascript .= 'var elements = [];';
$javascript .= "for (i=1;i<=$count;i++)";
$javascript .= "  elements.push(this.browserbot.findElement('/html/body/div/div/input['+i+']').id);";
#the last thing it should do is output a string, which Selenium will return to you
$javascript .= "elements.join(',');";

my $idString = $selObj->get_eval($javascript);

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):

var elements = eval_xpath('/html/body/div/div//input',this.browserbot.getCurrentWindow().document);
var results = [];
for (i=0;i<elements.length;i++){
  results.push(elements[i].id);
}

results.join(',');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文