ChromeDriver Laravel Dusk Set Download File Path

2019-08-16 06:45发布

问题:

Hi all wanted to ask how to set chromeDriver options default path to a specific directory so that if one downloads file using laravel dusk headless driver, files downloads to the directory.

Thanks in advance.

回答1:

Try this:

$this->browse(function (Browser $browser) {
    $url = $browser->driver->getCommandExecutor()->getAddressOfRemoteServer();
    $uri = '/session/' . $browser->driver->getSessionID() . '/chromium/send_command';
    $body = [
        'cmd' => 'Page.setDownloadBehavior',
        'params' => ['behavior' => 'allow', 'downloadPath' => '/your/download/path']
    ];
    (new \GuzzleHttp\Client())->post($url . $uri, ['body' => json_encode($body)]);

    $browser->visit('/');
});

Replace /your/download/path with the actual path.

This example requires the guzzlehttp/guzzle package to execute the POST request.

There are other solutions without external dependencies:
How do I send a POST request with PHP?