gpt4 book ai didi

xpath - 无法在 Selenium 中获取第 n 个节点

转载 作者:行者123 更新时间:2023-12-03 14:39:39 24 4
gpt4 key购买 nike

我尝试编写 xpath 表达式,以便我的测试不会因小的设计更改而中断。因此,我编写了自己的表达式,而不是 Selenium IDE 生成的表达式。

这里有一个问题:

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

这个表达式根本不起作用。名为“问题”的输入节点分布在整个页面中。他们不是 sibling 。

我试过使用中间表达式,但它也失败了。
(//input[@name='question'])[2]
error = Error: Element (//input[@name='question'])[2] not found

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

根据 XPath docs ,位置谓词必须通过节点集中的位置过滤,所以它必须找到第七个 input名称为 'question' .在 Selenium 中,这不起作用。 CSS 选择器 ( :nth-of-kind ) 也没有。

我必须写一个表达式来过滤他们共同的 parent :
//*[contains(@class, 'question_section')][7]//input[@name='question']

这是特定于 Selenium 的问题,还是我以错误的方式阅读规范?我该怎么做才能使表达更短?

最佳答案

Here's an issue:

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

This expression doesn't work at all.



这是一个常见问题 .
[]优先级高于 // .

上面的表达式选择每个 input带有 @name = 'question' 的元素,这是其父级的第 7 个 child ——显然是 input 的父级文档中未显示的元素没有那么多 input children 。

使用 (注意括号):
(//input[@name='question'])[7]

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

编辑 :

了解 Selenium 的人 (Dave Hunt) 建议将上述表达式用 Selenium 写成:
xpath=(//input[@name='question'])[7]

关于xpath - 无法在 Selenium 中获取第 n 个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3369342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com