如何解决“不是有效的Xpath表达式”柏树错误?

发布于 2025-02-06 11:52:48 字数 289 浏览 0 评论 0原文

我使用此字符串来找到一个元素:

cy.xpath('//*[text()="search ${keyword}")]')

当我运行测试时,它显示了控制台的正确结果,但也向我显示了此错误:

无法在“文档”上执行'评估':字符串'//*[text()=“ search $ {键word}”)]'不是有效的xpath表达式。<<<<<<<<< /p>

I used this string to find an element:

cy.xpath('//*[text()="search ${keyword}")]')

And when I run my test, it shows me the right result in console but also it shows me this error:

Failed to execute 'evaluate' on 'Document': The string '//*[text()="search ${keyword}")]' is not a valid XPath expression.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小忆控 2025-02-13 11:52:48

我使用了包含,并且可以工作。
我的XPath中有一个图标,它找不到文本。

cy.xpath(`//*[contains(text(), 'search ${keyword}')]`)

I used contains and it works.
there is an icon in my XPath and it couldn't find just the text.

cy.xpath(`//*[contains(text(), 'search ${keyword}')]`)
苄①跕圉湢 2025-02-13 11:52:48

您原始问题的问题

是实际的语法错误是由于无关的

cy.xpath('//*[text()="search ${keyword}")]')
                                        ^

删除该字符以消除语法错误。

您的自我答复

您的解决方案,

cy.xpath(`//*[contains(text(), 'search ${keyword}')]`)

您可能会认为它可能会做的。 (它仅检查 first text()每个元素的子女的子字符串。)有关详细信息,请参见此广泛的解释。

根据您的要求,使用

cy.xpath(`//*[text()[contains(., 'search ${keyword}')]]`)

或使用链接说明中显示的其他变体。

Issue with your original question

The actual syntax error is due to an extraneous ):

cy.xpath('//*[text()="search ${keyword}")]')
                                        ^

Delete that character to eliminate the syntax error.

Issue with your self-answer

Your solution,

cy.xpath(`//*[contains(text(), 'search ${keyword}')]`)

doesn't do what you likely think it does. (It's only checking for substrings within the first text() node child of every element.) For full details, see this extensive explanation.

Instead, use

cy.xpath(`//*[text()[contains(., 'search ${keyword}')]]`)

or the other variation shown in the linked explanation per your requirements.

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