php exec puppeteer脚本无输出

时间:2018-07-02 15:02:21

标签: php macos puppeteer

我使用php运行了此脚本,只是为了获取返回的值,但空白。 如果我直接在命令行上运行节点,则它具有一个值。我缺少什么?

代码由在网页内抓取div组成。

Php

<?php
$command = "/usr/local/bin/node /night/pup.js";
exec($command, $output, $return_var);
print_r($output);
//empty array
?>

端子:

/usr/local/bin/node /night/pup.js
//123456

pup.js

const puppeteer = require('puppeteer');

var weight_lb = 200;

(async () => {
    const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
    const page = await browser.newPage();
    await page.goto('http://xxx.xxx/xxx/x.html');

    await page.evaluate(function() {
        document.querySelector('#initialWeightField').value = '';
    })

    await page.type('#nothing', "1");

    const text = await page.evaluate(() => 
      document.querySelector('#finalWeightField').textContent);
    await console.log("success");
    await console.log(text);
    //Only for testing
    //await page.screenshot({path: 'example1.png',fullPage:true});
    await browser.close();

})();

1 个答案:

答案 0 :(得分:1)

  

.textContent更改为.value

const text = await page.evaluate(() => document.querySelector('#finalWeightField').value);