Puppeteer 查询选择器 - 如何获得第二场比赛

Fer*_*ito 1 javascript css-selectors node.js puppeteer

<table><tr><td>firstContent</td><td>secondContent</td></tr></table>
Run Code Online (Sandbox Code Playgroud)

使用 puppeteer 查询此表page.$evalI 检索firstContent。我将如何检索secondContent

const value = await page.$eval('table tr td', el => { return el.innerHTML });
Run Code Online (Sandbox Code Playgroud)

Tho*_*orf 8

你可以这样使用:nth-child

const value = await page.$eval('table tr td:nth-child(2)', el => { return el.innerHTML });
Run Code Online (Sandbox Code Playgroud)

对于更复杂的表达式,您还可以使用 中的document.querySelectorAll函数page.evaluate,然后像这样选择第二个元素:

const value = await page.evaluate(
    () => document.querySelectorAll('table tr td')[1].innerHTML
);
Run Code Online (Sandbox Code Playgroud)