Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppeteer howto find element within parent element

with cypress I can find child element within element as following:

cy.get('div#Login_form).within(() => {
  cy.get('input[name="human[email]"]').type('John')
  cy.get('input[name="human[password]"]').type('123456')
})

Is there any equivalence in Puppeteer for within()?

Thanks!

like image 416
mCY Avatar asked Jan 26 '23 22:01

mCY


1 Answers

Well one thing you can do is to declare your CSS selector paths as follows:

await page.type('div#Login_form > input[name="human[email]"]', 'John');
await page.type('div#Login_form > input[name="human[password]"]', '123456');

Another alternative, which might prove easier to read (even if it does mean more lines of code) would be to do the following:

// 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');
like image 120
AJC24 Avatar answered Jan 31 '23 23:01

AJC24