0

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.

Ankit Jain
  • 603
  • 2
  • 6
  • 17

1 Answers1

0

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?

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
  • the code you gave is working fine when site directly download the file. But when the link to download file uses another tab it doesn't work. Is there any other possibility or param to define and download the file. Thanks. – Ankit Jain May 18 '18 at 13:08
  • Manually opening the file url works for me: `$browser->visit($browser->element('a')->getAttribute('href'));` – Jonas Staudenmeir May 18 '18 at 14:36