简体   繁体   English

具有索引的xpath无法用作选择器

[英]xpath with index not working as a selector

The page has 3 buttons with a certain text {text}. 该页面有3个带有特定文本{text}的按钮。 I want to be able to click on the first such button. 我希望能够单击第一个这样的按钮。

I tested an xpath on chrome console with the index and that gives out the correct button. 我在带有索引的chrome控制台上测试了xpath,并给出了正确的按钮。

$x("//button[contains(text(), 'sometext')]")[0] $ x(“ // button [包含(text(),'sometext')]”“)[0]

But when I use the exact same xpath as a selector in my nightwatch code, it doesn't seem to find it. 但是,当我在夜间代码中使用与选择器完全相同的xpath时,似乎找不到它。

Whereas

selector: '//button[contains(text(), "sometext")]' 选择器:'// button [包含(text(),“ sometext”)]'

does seem to find 3 buttons but when I add [0] (as above) it fails to find the element. 的确似乎找到3个按钮,但是当我添加[0](如上所述)时,它找不到元素。

I would try [1] instead of [0]. 我会尝试[1]而不是[0]。 Xpath is 1 based. Xpath基于1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM