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

使用JavaScript程式碼在Laravel控制器中執(zhí)行Puppeteer操作
P粉098417223
P粉098417223 2023-08-28 09:40:29
0
1
719
<p>我正在使用Puppeteer從laravel控制器啟動(dòng)瀏覽器,但我不知道如何從控制器執(zhí)行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>當(dāng)我嘗試在node shell中只運(yùn)行程式碼的這部分:</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>它可以正常工作,我也可以看到打開了一個(gè)瀏覽器。當(dāng)我嘗試運(yùn)行整個(gè)函數(shù)時(shí),它不起作用。所以我認(rèn)為我在控制器中運(yùn)行JS程式碼的方式是錯(cuò)誤的。 </p>
P粉098417223
P粉098417223

全部回覆(1)
P粉077701708

我為JavaScript程式碼建立了一個(gè)新的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)進(jìn)行了更改:

$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();

這對(duì)我有用

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