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]'); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.