Document.querySelector()返回未定义的值

时间:2019-12-17 15:50:44

标签: javascript class puppeteer queryselector

我正在使用puppeteer开发一个网络抓取程序,该程序应该从网站收集文本。 我制作此类是为了从“ #identifierId> option:nth-​​child(1)”中收集文本并将其存储为对象属性,但是它返回一个未定义的值:我缺少什么?

async getText () {

   await this.page.waitForSelector('#identifierId> option:nth-child(1)');

   this.findText = await this.page.evaluate(() => {
        this.text = this.document.querySelector('#identifierId> option:nth-child(1)').innerText 
        return this.text
   })

   this.ArrayObject[0].text = this.findText.text

}

1 个答案:

答案 0 :(得分:1)

请尝试:

const escpos = require('escpos');

    // Select the adapter based on your printer type
    const device  = new escpos.USB();

    const printer = new escpos.Printer(device);


    const cartItems = [
        {category: 'test', price: 80, quantityToSell: 2, title: 'Hosting'},
        {category: 'test1', price: 820, quantityToSell: 63, title: 'Mouse'},
        {category: 'test00', price: 60, quantityToSell: 20, title: 'Sale'},
        {category: 'dvhfgnfgjfjg', price: 20, quantityToSell: 8, title: 'Keyboards'},
        {category: 'dvhfgnfgjfjg', price: 10, quantityToSell: 4, title: 'Keyss'},
        {category: 'dvhfgnfgjfjg', price: 70, quantityToSell: 1, title: 'Test'},
        {category: 'dvhfgnfgjfjg', price: 500, quantityToSell: 12, title: 'Whale oil'},
        {category: 'dvhfgnfgjfjg', price: 560, quantityToSell: 22, title: 'Papers'},
    ]

    // get total per line items
    const totalPerItemList = (item) => {

        let totalPerItem = 0

        totalPerItem = item.quantityToSell * item.price


        return totalPerItem
    }

    // get the total price
    let total = 0;
    for (let cartItem of cartItems) {
        var unitSum  = cartItem.quantityToSell * cartItem.price
        total += unitSum

    }

    // Create our html template, could be an html file on it's own
    const TestTable = `
    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Testing Title for table</title>            
        </head>   
        <body>
            <div class="invoice-box">
                <table class="receipt-table" cellpadding="0" cellspacing="0" border="0">
                    <thead>
                        <tr class="heading">
                            <th>Item</th>
                            <th>Quantity</th>
                            <th>Unit Price</th>
                            <th>Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${cartItems.map(item =>
                            `
                                <tr>
                                    <td>${item.title}</td>
                                    <td>${item.quantityToSell}</td>
                                    <td>${item.price}</td>
                                    <td>${totalPerItemList(item)}</td>
                                </tr>
                            `
                        )}
                    </tbody>
                    <tfoot>
                        <tr>
                            <td>
                                TOTAL:${total}
                            </td>
                        </tr>                                                   
                    </tfoot>
            </table>
            </div>            
        </body>     
    </html>
    `


    const htmlToText = require('html-to-text');

    const text = htmlToText.fromString(TestTable, {
        wordwrap: false,
        tables: ['.receipt-box', '.receipt-table']
    });


    device.open(function(err){

        printer
            .font('a')
            .align('ct')
            .style('bu')
            .size(1, 1)
            .text('Printing Tables Dynamically with epos')
            .text('||||||||||||||||||||||||||')
            .text(text)
            .text('||||||||||||||||||||||||')
            .text('========================')
            .cut()
            .close()
    });

说明- 基本上,在您的this.findText函数中,您实际上已经返回了 this.text ,这意味着this.findText实际上已经是您试图将this.ArrayObject [0] .text设置为的值。