Queridos todos, tengo un problema con el selector en el titiritero.
Tengo este código.
for(var k= 0 ; k<= 21 ; k++) { const text = await page.evaluate(() => { document.querySelector( 'div.ui-table__row:nth-child('+k+') > a:nth-child(1) > div:nth-child(2)' ).textContent }) console.log(text); }El problema cuando trato de ejecutar este fragmento es que no tengo k definido pero creo que es correcto.
Error: Evaluation failed: ReferenceError: k is not defined¿Cómo puedo solucionar este problema? Saludos
k debe pasarse/inyectarse explícitamente en evaluate()
const k = 'foo' await page.evaluate(k => {...}, k)Cambiar a:
for(var k= 0 ; k<= 21 ; k++) { const text = await page.evaluate((nth) => { document.querySelector( 'div.ui-table__row:nth-child('+nth+') > a:nth-child(1) > div:nth-child(2)' ).textContent }, k) console.log(text); }De acuerdo con este documento , debe pasar la variable como argumento a la page.evaluate así:
const result = await page.evaluate((x) => { // 1. Define "x" to get value in the step 2 return Promise.resolve(8 * x); // 3. Return the result }, 7); // 2. Pass 7 to "x" console.log(result); // prints "56"Su fragmento se verá así:
for(let k = 0; k <= 21; k++) { const text = await page.evaluate((nth) => { return document.querySelector( 'div.ui-table__row:nth-child('+ nth +') > a:nth-child(1) > div:nth-child(2)' ).textContent; // Return `textContent` to "out side" }, k) console.log(text); }