'How can I get an element by xpath?
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
Solution 1:[1]
Looks like your puppeteer version may be outdated
page.$x()
is new to 1.0.0
Solution 2:[2]
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 https://github.com/GoogleChrome/puppeteer/
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
Solution 3:[3]
Following should work
let el = await page.xpath('//*[@id="readium-right-panel"]/ul/li[1]');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Amature101 |
Solution 2 | Bhargav Mantha |
Solution 3 | joy |