Check if element exist and get attribute

I can't figure out why this piece of code not working:

var stock;
var stockEL = await page.$eval('.qty tbody tr td:nth-of-type(1) span');
if ( stockEL !== null) {
stock = stockEL.getAttribute('data-tip');
}

I am trying to use in Puppeteer. I would like to check if a span of first td exists, if yes, read the span attribute, if not exists do nothing.

 Error: Error: failed to find element matching selector ".qty tbody tr td:nth-of-type(1) span"

Upvotes: 0

Views: 1511

Answers (1)

Yusdel Morales
Yusdel Morales

Reputation: 36

try this variant await page.$(selector) or page.waitForSelector. Remember that $eval and $$eval needs a function as it's second argument.

[Example eval]

const data = await page.$$eval('selector', el => { /*code*/ });

[Example waitForSelector]

await page.waitForSelector('body > div.wrapper > aside > section > ul > li:nth-child(4) > a > span');

Here some useful links

Upvotes: 0

Related Questions