How to use xpath in chrome headless+puppeteer evaluate()?

后端 未结 2 1211
自闭症患者
自闭症患者 2020-12-09 05:26

How can I use $x() to use xpath expression inside a page.evaluate() ?

As far as page is not in the same context, I tried $x()

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 05:52

    If you insist on using page.$x(), you can simply pass the result to page.evaluate():

    const example = await page.evaluate(element => {
      return element.textContent;
    }, (await page.$x('//*[@id="result"]'))[0]);
    

提交回复
热议问题