gpt4 book ai didi

node.js - puppeteer如何在父元素中查找元素

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:37 28 4
gpt4 key购买 nike

使用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');

关于node.js - puppeteer如何在父元素中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000919/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com