我该如何检查操纵符是否存在于选择器中?

时间:2019-11-02 20:36:59

标签: javascript node.js puppeteer

在puppeteer中,如何检查例如#idProductType是否存在,如果不存在,则将producttype设置为“”?我尝试了很多事情,但是没有用。预先感谢。

            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);
            }

5 个答案:

答案 0 :(得分:5)

返回innerText或返回空字符串:

let productType = await page.evaluate(() => {
  let el = document.querySelector(".foo")
  return el ? el.innerText : ""
})

答案 1 :(得分:2)

如果DOM中没有特定选择器的元素,

querySelector()返回null

因此您可以编写简单的辅助函数:

const getInnerTextForSelector = (selector) => {
    const element = document.querySelector(selector);
    if (element)
        return element.innerText;
    return '';
};

例如运行#idProductType选择器:

const producttype = getInnerTextForSelector('#idProductType');

或者您可以编写将在puppeteer Page和ElementHandle上运行的助手:

const getElementForSelector = async (page, selector) => {
    return (await element.$(selector)) || undefined;
};

export const getInnerText = async (page, selector) => {
    const elementForSelector = await getElementForSelector(page, selector);
    try {
        if (elementForSelector)
            return (
                (await elementForSelector.evaluate(element => {
                    return element.innerText;
                })) || ''
            );
    } catch {
        return '';
    }
};

然后为#idProductType选择器运行:

const producttype = await getInnerText(page, '#idProductType');

答案 2 :(得分:2)

可能更适合此类用例的另一种方法:

{{1}}

答案 3 :(得分:1)

我想我找到了一种方法,我不知道这是否是最好的解决方案:

            var producttype = "";
            try {
                var producttype = document.querySelector('#idProductType').innerText;
            } catch (err) {
                console.log("The element didn't appear.")

答案 4 :(得分:0)

您可以使用类似于page.$(selector)document.querySelector(selector)

let producttype = (await page.$('#idProductType')) || "";