0

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!

Luciano Graziani
  • 521
  • 1
  • 8
  • 19
  • Don't use `FirefoxProfile`, use `FirefoxOptions` instead. – Florent B. Dec 04 '17 at 15:37
  • If I'm not mistaken, `FirefoxOptions` doesn't exist ;( – Luciano Graziani Dec 04 '17 at 16:14
  • If it's not implemented in the php client, then set the preferences directly in the capabilities. – Florent B. Dec 04 '17 at 16:17
  • Sorry for my late reply. I couldn't make it work. As you said, instead of passing those configs through the `$profile` object, I passed them directly to the `$caps` with the `setCapability`. – Luciano Graziani Dec 06 '17 at 11:45
  • Using the FirefoxProfile class the php web driver updates the firefox config. I checked in the about:config page and I have everything I need. (And it works, parcially. When I save the file manually, Firefox stores it in the configured folder). – Luciano Graziani Dec 22 '17 at 13:11

1 Answers1

1

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!

Luciano Graziani
  • 521
  • 1
  • 8
  • 19