I want to get selector from nodelist for specific filtering to pass to puppeteer waitForSelector that need css selector. i expect cause apple is 2nd child. i expect get button:nth-child(2)
const fruits = document.querySelectorAll('button');
for (const fruit of fruits) {
if (fruit.textContent == 'Apple') {
// get the element handle and pass to waitForSelector
console.log('Apple')
}
}
<button type="button">Orange</button>
<button type="button">Apple</button>
As suggest from andy, i solved it this ways. credit to andy
const fruits = document.querySelectorAll('button');
for (const fruit of fruits) {
if (fruit.textContent == 'Apple') {
fruit.setAttribute('data-type', 'Apple');
break
}
}
return document.querySelector('button[data-type=Apple]')
<button type="button">Orange</button>
<button type="button">Apple</button>