0

I'm trying to get the text inside of my value div with id staked (397.497836). It looks like this:

enter image description here

I'm coping the xpath of the element, which is: //*[@id="staked"]

In all of the examples i've seen on stackoverflow/github it seems that people are able to retrieve the text using textContent however my logging doesn't show that field available.

await page.waitForXPath('//*[@id="staked"]');
let handle = await page.$x('//*[@id="staked"]');
console.log(handle[0]);

 _remoteObject: {
    type: 'object',
    subtype: 'node',
    className: 'HTMLDivElement',
    description: 'div#staked.value',
    objectId: '-6241729959890640645.3.7'
  },

For context, this isn't static text, as i'm passing it from my React component. Any ideas how I could do this differently to obtain the text?

Harrison
  • 5,095
  • 7
  • 40
  • 60
  • 1
    What's the point of using an xpath when you have the id? `page.$("#staked")` seems easier, and if you want the text, it's the normal way, `page.$eval("#staked", el => el.textContent))`. – ggorlen Mar 04 '22 at 20:46
  • 1
    Does this answer your question? [how to get text inside div in puppeteer](https://stackoverflow.com/questions/55237748/how-to-get-text-inside-div-in-puppeteer) – ggorlen Mar 04 '22 at 20:47

1 Answers1

0

now you need to extract that value from the handle with .evaluate

and as ggorlen pointed out, you can use id:

// xpath
const el = await page.waitForXPath('//*[@id="staked"]');
const content1 = await el.evaluate(el => el.textContent);
console.log('xpath', content1);

// id
const content2 = await page.$eval("#staked", el => el.textContent);
console.log('id', content2);
traynor
  • 5,490
  • 3
  • 13
  • 23
  • This was something I had tried prior my issue actually was that I had to sleep for roughly a second before the values actually were there as prior to that a spinning GIF logo was in the spot. Appreciate the help. – Harrison Mar 07 '22 at 14:11