Luciano Graziani
Luciano Graziani

Reputation: 531

How to download a file with the PHP Webdriver for Selenium on Firefox 58++

I been trying to download a file with latests versions of Firefox using the PHP Webdriver for Selenium but I cannot make it work. This is the code I have in my phpunit bootstrap.php file for the WebDriver configuration for Firefox:

$profile = new FirefoxProfile();
$caps = DesiredCapabilities::firefox();

$profile->setPreference('browser.download.folderList', 2);
$profile->setPreference('browser.download.manager.showWhenStarting', false);
$profile->setPreference('browser.download.dir', __DIR__.'/temp');
$profile->setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf');

$caps->setCapability(FirefoxDriver::PROFILE, $profile);

RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);

Some of those preferences, like browser.helperApps.neverAsk.saveToDisk doesn't exist on the about:config page. I can add them manually, but even doing that, I cannot make Firefox download a file to an specific folder without asking me if I want to save it.

Maybe is not possible anymore?

Thank you!

Upvotes: 0

Views: 3421

Answers (1)

Luciano Graziani
Luciano Graziani

Reputation: 531

Ok. I found my error. I had this header configured for sending pdfs:

header('Content-Disposition: attachment; filename="filename.pdf"');

Sadly, the attachment option makes Firefox always open the Save as window. Changing it to inline solves the problem.

By the way, for Firefox 57++, this is the correct configuration:

$profile = new FirefoxProfile();
$caps = DesiredCapabilities::firefox();

$profile->setPreference('browser.download.folderList', 2);
$profile->setPreference('browser.download.dir', __DIR__.'/temp');
$profile->setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf');
$profile->setPreference('pdfjs.enabledCache.state', false);

$caps->setCapability(FirefoxDriver::PROFILE, $profile);

RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);

Credits to https://stackoverflow.com/a/47707635/2862917

PS: on Windows, the browser.download.dir must have \ instead of / for the path!

Upvotes: 1

Related Questions