无法在Selenium中获取第n个节点 [英] Can't get nth node in Selenium

查看:425
本文介绍了无法在Selenium中获取第n个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试写入xpath表达式,以便我的测试不会被小的设计更改打破。因此,不是Selenium IDE生成的表达式,而是我自己写的。



这里有个问题:

  // input [@ name ='question'] [7] 

这个表达式根本不工作。名为问题的输入节点分布在整个页面上。



我尝试过使用中间表达式,但它也失败了。

 (// input [@ name ='question'])[2] 
error =错误:Element(// input [@ name ='question'发现

这就是为什么我想Seleniun有错误的XPath实现。



根据 XPath文档,位置谓词必须过滤在 nodeset 中的位置,因此必须找到名为'question'的第七个输入 c>。在Selenium这不工作。 CSS选择器(:nth-​​of-kind )。



我必须编写一个表达式, parent:

  // * [contains(@class,'question_section')] [7] 'question'] 

这是一个Selenium特定的问题,

解决方案


这是一个问题:

  //输入[@ name ='question'] [7] 

这个表达式根本不起作用。


a 。。



c>



上述表达式选择每个输入 @name ='question',它是其父项的第7个子项,并且显然是文档中输入元素的父项显示没有太多输入的儿童。



使用 ):

 (// input [@ name ='question'])[7] 

这将选择文档中满足谓词条件的第7个元素 input



编辑

人们知道Selenium上面的表达式用Selenium写成:

  xpath =(// input [@ name ='question']) 7] 


I try to write xpath expressions so that my tests won't be broken by small design changes. So instead of the expressions that Selenium IDE generates, I write my own.

Here's an issue:

//input[@name='question'][7]

This expression doesn't work at all. Input nodes named 'question' are spread across the page. They're not siblings.

I've tried using intermediate expression, but it also fails.

(//input[@name='question'])[2]
error = Error: Element (//input[@name='question'])[2] not found

That's why I suppose Seleniun has a wrong implementation of XPath.

According to XPath docs, the position predicate must filter by the position in the nodeset, so it must find the seventh input with the name 'question'. In Selenium this doesn't work. CSS selectors (:nth-of-kind) neither.

I had to write an expression that filters their common parents:

//*[contains(@class, 'question_section')][7]//input[@name='question']

Is this a Selenium specific issue, or I'm reading the specs wrong way? What can I do to make a shorter expression?

解决方案

Here's an issue:

//input[@name='question'][7]   

This expression doesn't work at all.

This is a FAQ.

[] has a higher priority than //.

The above expression selects every input element with @name = 'question', which is the 7th child of its parent -- and aparently the parents of input elements in the document that is not shown don't have so many input children.

Use (note the brackets):

(//input[@name='question'])[7]

This selects the 7th element input in the document that satisfies the conditions in the predicate.

Edit:

People, who know Selenium (Dave Hunt) suggest that the above expression is written in Selenium as:

xpath=(//input[@name='question'])[7]

这篇关于无法在Selenium中获取第n个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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