英文:
How to get element of show button
问题
Here is the translated content:
我想使用 Cypress 在我的应用程序中显示和隐藏小部件,但我找不到元素,因为这里有很多显示和隐藏按钮,这是我想要自动化的弹出屏幕截图。
我尝试了不同的方法,但都没有成功。我尝试了以下代码,但没有成功:
it('点击显示按钮', () => {
cy.wait(5000)
cy.get('.form-row').find('input[value="显示"]').click()
})
我尝试了不同的方式,从类、ID 和属性获取它,但没有找到任何解决方案,我期望能成功点击显示按钮。
英文:
I want to Show and hide widget in my application using cypress but i didn't find element because there so many show and hide button here is the popup Screen shot which want to automate
I tried different ways but didn't working. i tried it with below code but didn't work
it('Click on Show button', () => {
cy.wait(5000)
cy.get('.form-row').find('input[value="Show"]').click()
})
I tried different ways i get it from class, id and attribute did not find any solution i am expecting to click on show button successfully
答案1
得分: 4
以下是翻译好的部分:
选择正确的行.form-row
的最简单方法是使用.contains()
来查找文本Balance Change Alert
。
cy.contains('.form-row', 'Balance Change Alert') // 选择整行
.find('input[value="Show"]') // 进一步查找按钮
.click()
一旦你有了这一行,它的所有元素都在其中,所以如果有多个步骤,你可以使用.within()
:
cy.contains('.form-row', 'Balance Change Alert')
.within(() => { // 所有在此处的查询都限定在所需的行内
cy.get('input[value="Hide"]').should('have.css', 'k-state-active')
cy.get('input[value="Show"]').click()
cy.get('input[value="Show"]').should('have.css', 'k-state-active')
})
英文:
The easiest way to choose the right row .form-row
is by the text Balance Change Alert
using .contains()
.
cy.contains('.form-row', 'Balance Change Alert') // whole row is selected
.find('input[value="Show"]') // drill down to button
.click()
Once you have the row, all it's elements are within it so you could use .within()
if you have several steps:
cy.contains('.form-row', 'Balance Change Alert')
.within(() => { // all queries in here are limited to the required row
cy.get('input[value="Hide"]').should('have.css', 'k-state-active')
cy.get('input[value="Show"]').click()
cy.get('input[value="Show"]').should('have.css', 'k-state-active')
})
答案2
得分: 3
你还可以在.get()
后面添加.contains()
,以起到筛选的作用。
其他选项以<label>
开头的是.closest()
和.parents()
。
cy.get('.form-row')
.contains('Balance Change Alert')
.should('have.length', 1) // 只返回一个行
.find('input[value="Show"]')
.click()
cy.contains('Balance Change Alert') // 选择标签
.closest('.form-row')
.should('have.length', 1) // 只返回一个行
.find('input[value="Show"]')
.click()
cy.contains('Balance Change Alert') // 选择标签
.parents('.form-row')
.should('have.length', 1) // 只返回一个行
.find('input[value="Show"]')
.click()
英文:
You can also add .contains()
after .get()
to act like a filter
cy.get('.form-row')
.contains('Balance Change Alert')
.should('have.length', 1) // only one row is returned
.find('input[value="Show"]')
.click()
Other options starting at the <label>
are .closest()
and .parents()
cy.contains('Balance Change Alert') // selects the label
.closest('.form-row')
.should('have.length', 1) // only one row is returned
.find('input[value="Show"]')
.click()
cy.contains('Balance Change Alert') // selects the label
.parents('.form-row')
.should('have.length', 1) // only one row is returned
.find('input[value="Show"]')
.click()
答案3
得分: 1
"didn't work"的确切错误消息是什么将会很有帮助。从你添加的代码片段来看,我怀疑它会出现以下错误:"cy.click() 只能在单个元素上调用。" 因为这段代码:
cy.get('.form-row').find('input[value="Show"]')
会返回所有的"Show"按钮。如果你只想点击其中一个,你需要缩小范围。最简单的方法是使用eq()来指定你想点击的按钮。根据你的截图,你已经突出显示了第二行,所以要点击第二行的"Show"按钮,可以使用eq(1)(因为索引从0开始),像这样:
it('Click on Show button', () => {
cy.wait(5000)
cy.get('.form-row').eq(1).find('input[value="Show"]').click()
})
英文:
It would be helpful to know what you mean by "didn't work", what error message do you get exactly. From the piece of code you added, my suspicion is that it fails with this error: "cy.click() can only be called on a single element." Because this:
> cy.get('.form-row').find('input[value="Show"]')
will return you all the Show buttons. If you want to click only one of them, you have to narrow it down. The easiest way would be using eq() to specify which one do you want to click on. On your screenshot you have the second row highlighted, so for clicking on the Show button in the second row, us eq(1) (as indexing starts with 0), like this:
it('Click on Show button', () => {
cy.wait(5000)
cy.get('.form-row').eq(1).find('input[value="Show"]').click()
})
答案4
得分: 0
你可以使用 cy.parents()
结合 CSS 选择器来找到包含文本和显示/隐藏按钮的父元素。注意: cy.parents()
返回一个列表,所以我们仍然需要使用 cy.eq()
。
cy.contains('Balance Change Alert')
.parents('.form-row')
.eq(0) // 假设我们只会找到一个符合条件的父元素
.find('input[value="Show"]')
.click();
你可以修改 cy.contains()
来找到对应的行,假设所有其他行的HTML结构类似。
英文:
You can use cy.parents()
with a CSS selector to find the parent that contains both the text and the show/hide buttons. Note: cy.parents()
yields a list, so we'll still need to do cy.eq()
cy.contains('Balance Change Alert')
.parents('.form-row')
.eq(0) // assumes we only find one parent that meets this criteria
.find('input[value="Show"]')
.click();
You can modify the cy.contains()
to find the corresponding row, assuming that all other rows have similar HTML structure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论