如何在 Firefox 58++ 上使用 PHP Webdriver for Selenium 下载文件

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

我一直在尝试使用 PHP 用于 Selenium 的 Webdriver 下载带有最新版本 Firefox 的文件,但我无法使其工作。这是我的 phpunit bootstrap.php 文件中用于 Firefox 的 WebDriver 配置的代码:

$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);

about:config 页面上不存在某些偏好设置,例如 browser.helperApps.neverAsk.saveToDisk。我可以手动添加它们,但即使这样做,我也无法让 Firefox 在不询问我是否要保存的情况下将文件下载到特定文件夹。

也许不可能了?

谢谢!

好的。我发现了我的错误。我将此 header 配置为发送 pdf:

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

遗憾的是,attachment 选项使 Firefox 始终打开另存为 window。将其更改为 inline 即可解决问题。

顺便说一下,对于 Firefox 57++,这是正确的配置:

$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);

感谢 https://whosebug.com/a/47707635/2862917

PS:在 Windows 上,browser.download.dir 的路径必须有 \ 而不是 /