title: Selenium problems with PDF download in Firefox
body: <p>I'm working on an upgrade of our internal Java Selenium framework to the latest version 3.14.0 in combination with Firefox 61.0 and Geckodriver 0.21.0.</p>
  
  <p>I face problems when it comes to an automated download of a PDF file with Firefox. For example at this <a href="https://www.academic-conferences.org/pdf/download-info/eccws-2016-abstract-booklet/" rel="nofollow noreferrer">link</a> there is a download button where I can let selenium perform a click on. Instead of downloading the pdf, a built-in viewer will be opened. The preference <code>pdfjs.disabled</code> should deactivate the viewer so created the driver instance with a profile (and also tried some more preferences).</p>
  
  <pre><code>FirefoxProfile firefoxProfile = new FirefoxProfile();
  firefoxProfile.setAcceptUntrustedCertificates(true);
  firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
  firefoxProfile.setPreference("browser.download.folderList", 2);
  firefoxProfile.setPreference("network.cookie.cookieBehavior", 0);
  firefoxProfile.setPreference("network.cookie.alwaysAcceptSessionCookies", true);
  firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
  firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
  firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
  firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
  firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
  firefoxProfile.setPreference("browser.download.panel.shown", false);
  firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
  firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
  firefoxProfile.setPreference("browser.download.manager.useWindow", false);
  firefoxProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
  firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
  firefoxProfile.setPreference("pdfjs.disabled", true);
  
  FirefoxOptions firefoxOptions = new FirefoxOptions();
  firefoxOptions.setProfile(firefoxProfile);
  WebDriver webdriver = new FirefoxDriver(firefoxOptions);
  </code></pre>
  
  <p>Unfortunately with the used Firefox version this config has no effect. Unless you change <code>pdfjs.disabled</code> to <code>true</code> at runtime within the <code>about:config</code> page. Only when first changed at runtime, the pdf viewer is really disabled. Seems to be a bug in Firefox. Ok anyway, I found a way to change this at runtime with selenium.</p>
  
  <p>But now that the pdf viewer is skipped another <a href="https://i.stack.imgur.com/dmY3b.png" rel="nofollow noreferrer">popup</a> is interrupting the download. Selenium is not even aware of this popup.</p>
  
  <p>On the preferences page <code>about:preferences</code> under <a href="https://i.stack.imgur.com/QxZj7.png" rel="nofollow noreferrer">Applications</a> I can see now that for the Content Type <code>PDF document</code> the Action was changed to nothing, which seems to be the same as <code>Always ask</code>. Before it was <code>Preview in Firefox</code> but it has to be <code>Save File</code> in order to reach my goal which is to immediately download the PDF without any questions.</p>
  
  <p>Not with a single config you could possibly change this to "Save File". Am I wrong? But when I checked profile folders for differences I found out that there is a <code>handlers.json</code> that contains the settings from the picture above. It is read in during the browser startup and first adapted on browser close.
  <br>Normally selenium creates a new temporary profile folder for each new browser instance. If I want to have an influence on the desired setting, I need to define a custom profile. That was my thought. So I tried this:</p>
  
  <pre><code>firefoxOptions.addArguments("-profile", "/tmp/my.profile");
  </code></pre>
  
  <p>I think this is correct, but now yet another problem... becauses though Firefox creates the neccessary files in the given directory, the the communication between selenium and firefox fails. The browser window is opened and healthy, but Selenium is not able to start the session.</p>
  
  <pre><code>org.openqa.selenium.WebDriverException: connection refused 
  
              at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
              at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
              at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
              at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
              at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
              at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)
              at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
              at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
              at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
              at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
              at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
              at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
              at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
              at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
              at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
              at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
              at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
              at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
              at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
              at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
              at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
              at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:130)
              at org.openqa.selenium.firefox.FirefoxDriver.&lt;init&gt;(FirefoxDriver.java:140)
              …
  </code></pre>
  
  <p>So the general question is: <br><strong>How can I force Firefox (controlled by selenium) to download the PDF file to my file system when clicked on the button?</strong> <br>Any possibilities are reasonable.</p>
  
  <p>As I am running out of ideas it would be great if somebody could help or at least confirm that these things are problems with Firefox.</p>