问题
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?
来源:https://stackoverflow.com/questions/48888036/chromedriver-laravel-dusk-set-download-file-path