问题描述
我尝试写入xpath表达式,以便我的测试不会被小的设计更改打破。因此,不是Selenium IDE生成的表达式,而是我自己写的。
这里有个问题:
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 foundThat'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?
解决方案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个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!