Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer Getting List of Elements with Same Selector

Tags:

Background:

Using NodeJS/CucumberJS/Puppeteer to build end-to-end regression test for an emberJS solution.

Problem:

Selecting (page.click) and getting textContent of one of the elements when there are several dynamic elements with the same selector? (In my case, I have 4 elements with the same selector = [data-test-foo4="true"])

I know, that with:

const text = await page.evaluate( () => document.querySelector('[data-test-foo4="true"]').textContent ); 

I can get the text of the first element, but how do I select the other elements with the same selector? I've tried:

var text = await page.evaluate( () => document.querySelectorAll('[data-test-foo4="true"]').textContent )[1]; console.log('text = ' + text); 

but it gives me 'text = undefined'

Also, the following:

await page.click('[data-test-foo4="true"]'); 

selects the first elements with that selector, but how can I select the next one with that selector?

like image 802
Huckleberry Carignan Avatar asked Sep 07 '18 14:09

Huckleberry Carignan


People also ask

How do you get a list of puppeteer elements?

You can get the elements by using the class in puppeteer, but the puppeteer does not understand what is class or id; so you have to use the CSS format to make the puppeteer understand it. Use . (dot) before the class name to denote that the following is class.

How do you get the puppeteer element text?

We can get element text in Puppeteer. This is done with the help of the textContent property. This property of the element is passed as a parameter to the getProperty method.


1 Answers

You can use Array.from() to create an array containing all of the textContent values of each element matching your selector:

const text = await page.evaluate(() => Array.from(document.querySelectorAll('[data-test-foo4="true"]'), element => element.textContent));  console.log(text[0]); console.log(text[1]); console.log(text[2]); 

If you need to click more than one element containing a given selector, you can create an ElementHandle array using page.$$() and click each one using elementHandle.click():

const example = await page.$$('[data-test-foo4="true"]');  await example[0].click(); await example[1].click(); await example[2].click(); 
like image 63
Grant Miller Avatar answered Oct 12 '22 05:10

Grant Miller