1

I've followed this question and searched around the internet but there seems to be solutions only for Java, Python and JavaScript.

I'm trying to download a file using Chrome as my WebDriver in Selenium and having it executed with the headless option enabled.

After checking the solution for JavaScript in this post, I thought it would be very straight forward with PHP but my Selenium server always returns a null response.

I've tried different approaches including using curl to enable the download directly:

$params = [
    'cmd' => 'Page.setDownloadBehavior',
    'params' => [
        'behavior' => 'allow',
        'downloadPath' => 'path/to/documents/'
    ]
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt(
    $curl,
    CURLOPT_URL,
    'http://localhost:4444/wd/hub/session/'.$driver->getSessionID().'/chromium/send_command'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));

$raw_results = trim(curl_exec($curl));

$this->driver->get('https://example.com/document.pdf');

The response I receive is:

{"sessionId":"7a724b52a9d52d3c89481345351fea117","status":0,"value":null}

Has anyone managed to download a file in headless mode using Chrome?

CIRCLE
  • 4,501
  • 5
  • 37
  • 56

0 Answers0