2 回答

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
.find()
用于單個(gè)元素的搜索,但僅限于該元素的操作
.within()
允許您更改搜索子元素的范圍并直接使用 調(diào)用它們cy.get('subelementSelector')
,也可以使用它們。缺點(diǎn)是,您無法從父元素范圍之外調(diào)用元素。
那么第三種方法就是?。cy.get('elementSelector').then(element=>{//some code})
- 這允許您將元素傳遞給函數(shù)以供使用。您可以使用 搜索其中的子元素cy.get(element).find('subelementSelector')
。您還可以對(duì)位于父元素范圍之外的元素使用常用命令。它的語法更長(zhǎng),但范圍更大。
編輯:?澄清一下?.find()
- 允許元素的單次使用?.within(passedFunction()=>{})
- 將 passFunction 的 DOM 元素的范圍更改為子元素?.then(element=>{})
- 不會(huì)更改范圍,但會(huì)創(chuàng)建該變量的 JQ 變量,可在then 函數(shù)?cy.get('parentSelector childSelector')
- 是獲得與以下相同結(jié)果的 css 方法.find()

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
查找:
獲取特定選擇器的后代 DOM 元素。find() 方法始終與返回 DOM 元素的其他方法鏈接,并且永遠(yuǎn)不會(huì)與“cy”對(duì)象鏈接。
.find(selector)
.find(selector, options)
cy.get('.article').find('footer') // Yield 'footer' within '.article'
以內(nèi):
它對(duì)我們想要在父 Web 元素內(nèi)搜索 Web 元素的情況進(jìn)行排序。它將所有后續(xù) cy 命令的范圍限制在父元素內(nèi)。在特定的元素組(例如表單)中工作時(shí)很有用。這是用回調(diào)函數(shù)編寫的,例如。在(callbackFn)內(nèi)。
.within(callbackFn)
.within(options, callbackFn)
cy.get('.list').within(($list) => {}) // Yield the `.list` and scope all commands within it
// validate placeholder attributes
cy.get('.query-form').within(() => {
cy.get('input:first').should('have.attr', 'placeholder', 'Email')
cy.get('input:last').should('have.attr', 'placeholder', 'Password')
})
添加回答
舉報(bào)