简体   繁体   English

在 Cypress 中优化选择器

[英]Refining selectors in Cypress

I'm trying to learn best practice for selectors using Cypress.我正在尝试学习使用赛普拉斯的选择器的最佳实践。

I have an element buried within many tables within a frame.我有一个元素埋在一个框架内的许多表中。 This is an app I have no control over so cannot add custom selectors for Cypress.这是一个我无法控制的应用程序,因此无法为赛普拉斯添加自定义选择器。 The full Xpath is as follows:完整的Xpath如下:

/html/body/table/tbody/tr[1]/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr/td/a

Within the a tag is the link I wish to do a click() on.a标签内是我希望在其上执行click()的链接。

<a class="menubuttontext" style="cursor:pointer" href="javascript:menuClick(1)">Men's Apparel</a>

Can someone provide the 'best' (or at least relatively resilient) selector?有人可以提供“最佳”(或至少相对有弹性)的选择器吗? I don't wish to fall back to adding Xpath libraries - I am switching from many years working with Selenium so learning as I go.我不想回退到添加 Xpath 库 - 我正在从多年使用 Selenium 转换,所以像我一样学习 go。 I'm unclear as to how many attributes I can chain together or even how - I am digging through the documentation but some help would be much appreciated.我不清楚我可以将多少个属性链接在一起,甚至如何链接 - 我正在研究文档,但非常感谢一些帮助。

If you can rely on the text content of your link element, you may try with Cypress commandcy.contains() :如果您可以依赖链接元素的文本内容,您可以尝试使用赛普拉斯命令cy.contains()

cy.contains('a.menubuttontext', "Men's Apparel").click();

You can also use a regex for the 2nd argument to fine tune your search.您还可以对第二个参数使用正则表达式来微调您的搜索。

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

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