Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if selector exists in puppeteer?

In Puppeteer, how can I check if, for example, #idProductType exists and if not, set producttype to ""? I tried many many things but it doesn't work.

const urls = [myurls, ...]
const productsList = [];
for (let i = 0; i < urls.length; i++) {
    const url = urls[i];
    await Promise.all([
        page.goto(url),
        page.waitForNavigation({ waitUntil: 'networkidle0' }),
    ]);

    let products = await page.evaluate(() => {

  //here i want to test if #idProductType exists do : 
        let producttype = document.querySelector('#idProductType').innerText;
  //else 
        let producttype = "";
  //and same thing for other selectors

        let productsubtype = document.querySelector('#idProductSubType').innerText;
        let product = document.querySelector('#idProduct').innerText;
        let description = document.querySelector('td.js-orderModelVer').innerText;
        let reference = document.querySelector('td.text-nowrap').innerText;
        let prixpub = document.querySelector('td.text-nowrap.text-right.js-pricecol-msrp').innerText;
        let dispo = document.querySelector('td.text-nowrap.text-center.js-pricecol-availability').innerText;
        let retire = document.querySelector('td.js-retired-filler-cell').innerText;

        let results = [];
        results.push({
            producttype: producttype,
            productsubtype: productsubtype,
            product: product,
            description: description,
            reference: reference,
            prixpub: prixpub,
            dispo: dispo,
            retire: retire
        })
        return results
    })
    productsList.push(products);
}
like image 479
Cyri1 Avatar asked Nov 02 '19 20:11

Cyri1


People also ask

How do you use a puppeteer selector?

Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case. For this, our first job is to identify the element.

How do you know if an element exists in playwright?

To Fix check if an element exists on the page in Playwright. js, try { await page. waitForSelector(selector, { timeout: 5000 }) // ...`enter code here` } catch (error) {`enter code here` console. log("The element didn't appear.") }


3 Answers

return innerText or empty string if not found:

let productType = await page.evaluate(() => {
  let el = document.querySelector(".foo")
  return el ? el.innerText : ""
})
like image 55
pguardiario Avatar answered Sep 20 '22 13:09

pguardiario


You can use page.$(selector) which is similar to document.querySelector(selector)

let producttype = (await page.$('#idProductType')) || "";
like image 39
Mohammad Faisal Avatar answered Sep 20 '22 13:09

Mohammad Faisal


Puppeteer throws an error when could not find a matched element.

So to confirm the existence,

try {
  await page.$(selector)
  // Does exist
} catch {
  // Does not
}

Or to make whether it exists a flag

const exists = await page.$eval(selector, () => true).catch(() => false)
like image 40
SHION TONATIUH FUJIE Avatar answered Sep 21 '22 13:09

SHION TONATIUH FUJIE