使用cypress,我可以在
元素中找到子元素,如下所示:
cy.get('div#Login_form).within(() => {
cy.get('input[name="human[email]"]').type('John')
cy.get('input[name="human[password]"]').type('123456')
})
Puppeteer
中的 within()
是否有等效项?
谢谢!
您可以做的一件事就是声明 CSS 选择器路径,如下所示:
await page.type('div#Login_form > input[name="human[email]"]', 'John');
await page.type('div#Login_form > input[name="human[password]"]', '123456');
另一种可能更容易阅读的替代方案(即使它确实意味着更多行代码)是执行以下操作:
// Get the form element
const form = await page.$('div#Login_form');
// Get the email and password elements from the form
const email = await form.$('input[name="human[email]"]');
const password = await form.$('input[name="human[password]"]');
// Type the data into each element
await email.type('John');
await password.type('123456');
我是一名优秀的程序员,十分优秀!