I need to find any element in DOM by xpath. I already tried the following:
let el = await page.$x('//*[@id="readium-right-panel"]/ul/li[1]'); The returning error is:
TypeError: page.$x is not a function
3 Answers
Looks like your puppeteer version may be outdated
page.$x() is new to 1.0.0
the problem most probably might be because of the older puppeteer version. You might want to check the puppeteer version in your package.json file.
SMALL NOTE: npm i does not upgrade puppeteer.
if(puppeteer version >1.0.0)
###try this,###
suppose that //*[@id="ng-app"] is the global prefix then u add it before the Xpath variable. I have used interpolation for this.
await page.waitForXPath(`//*[@id="ng-app"]/${Xpath}`, { visible: true });//waiting for the xPath element to be visible const elementToClick = await page.$x(`//*[@id="ng-app"]/${Xpath}`); await elementToClick[0].click(); this is an example to click the element extracted of course.
in your case it will be
await page.waitForXPath('//*[@id="readium-right-panel"]/ul/li[1]'); let el =await page.$x(`//*[@id="readium-right-panel"]/ul/li[1]`); await el[0].click(); source:the API DOCS Of Puppeteer
Following should work
let el = await page.xpath('//*[@id="readium-right-panel"]/ul/li[1]');