国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

使用JavaScript代碼在Laravel控制器中執(zhí)行Puppeteer操作
P粉098417223
P粉098417223 2023-08-28 09:40:29
0
1
720
<p>我正在使用Puppeteer從laravel控制器啟動瀏覽器,但是我不知道如何從控制器運行JavaScript代碼</p> <p>我的函數(shù)如下:</p> <pre class="brush:php;toolbar:false;">$script = <<<SCRIPT const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('http://my-url.com'); const button = await page.$('button#button'); await button.evaluate( button => button.click() ); await page.waitForTimeout(5000); ... await browser.close(); })(); SCRIPT; $process = new Process(['node', '-e', $script]); $process->run(); $output = $process->getOutput();</pre> <p>當我嘗試在node shell中僅運行代碼的這部分時:</p> <pre class="brush:php;toolbar:false;">const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('http://my-url.com'); const button = await page.$('button#button'); await button.evaluate( button => button.click() ); await page.waitForTimeout(5000); ... await browser.close(); })();</pre> <p>它可以正常工作,我也可以看到打開了一個瀏覽器。當我嘗試運行整個函數(shù)時,它不起作用。所以我認為我在控制器中運行JS代碼的方式是錯誤的。</p>
P粉098417223
P粉098417223

全部回復(1)
P粉077701708

我為JavaScript代碼創(chuàng)建了一個新的js文件 script.js

// script.js

const puppeteer = require('puppeteer');
(async () => {
    const browser = await puppeteer.launch({headless: false});     
    const page = await browser.newPage();
    await page.goto('http://my-url.com');
    const button = await page.$('button#button');
    await button.evaluate( button => button.click() );
    await page.waitForTimeout(5000);
    ...
    await browser.close();
})();

而在控制器中,我已經(jīng)進行了更改:

$process = new Process(['path/to/node', 'path/to/script.js]);
$process->run();
if(!$process->isSuccessful()) {
  throw new ProcessFailedException($process);
}
$output = $process->getOutput();
$errors = $process->getErrorOutput();

這對我有用

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板