如何使用赛普拉斯选择和复制文本?

时间:2020-07-22 12:38:20

标签: cypress

我需要选择整个演员的名字;为此,我可以使用CSS定位器识别名称的位置,但是我不知道如何选择该文本。 (这类似于我们在按住鼠标左键并拖动到所需位置时执行的操作)

具体来说,就我而言,我正在尝试复制“ Ricky Gervais”,并且此选择器将我带到了它。如何选择和复制此文本?

cy.get('tbody > :nth-child(2) > :nth-child(2) > a')

enter image description here

2 个答案:

答案 0 :(得分:0)

我找到了一种符合我的目的的方法,以为我将来在这里分享给他人看

         cy.get(<selector-of-the-element-that-you-want-to-copy>).then(($temp)=>{
        const txt = $temp.text()
        cy.get(<selector-of-the-field/place-where-you-want-to-paste>).type(`${txt}`+'{enter}')

重要提示

.text()默认情况下不可用。为此,首先,您需要添加以下插件

npm install cypress-commands

可以在这两个非常有用的链接上找到更多信息

https://github.com/Lakitna/cypress-commands https://github.com/Lakitna/cypress-commands/blob/develop/docs/text.md

干杯!

答案 1 :(得分:-1)

可以使用纯JavaScript来选择网页上的纯文本: method declaration

还将当前选择复制到剪贴板: Selecting text in an element (akin to highlighting with your mouse)