Downloading a PDF using Selenium, Chrome and Python












2















I tried following previous posts on this topic such as these (post 1, post 2), but I'm still stuck.



My script has to log into a site using a set of credentials, then navigate through some drop down menus to select a report. Once the report is selected, a new window pops up where parameters must be adjusted to generate the report. Once the parameters are set, the same pop up window refreshes with the generated report in PDF format and is displayed using Chrome's built in PDF viewer. I was under the impression that passing certain options to the webdriver would disable this PDF viewer and simply download the file, but the PDF viewer is still being displayed and nothing is automatically downloaded. Surely I'm missing something or I wrote something incorrectly. Here's the jist of my code:



from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.plugins_disabled": ["Chrome PDF Viewer"]
}
)

browser = webdriver.Chrome(options = chrome_options)

driver = webdriver.Chrome()
driver.get(url)

#In between here are a bunch of steps here that navigates through drop down menus

#This step may not be necessary, but I figured I'd include it to address when the pop up window refreshes and displays the report in PDF format through Chrome's PDF viewer
driver.switch_to.window(driver.window_handles[1])


So, at this point, Chrome still displays the PDF viewer even though I disabled it earlier. Nothing is downloaded, so I'm wondering if I need to provide another line of code or perhaps something else.



Using Selenium version 3.141.0, Python 3.6.4, Chrome webdriver 2.45 on Windows 10.










share|improve this question



























    2















    I tried following previous posts on this topic such as these (post 1, post 2), but I'm still stuck.



    My script has to log into a site using a set of credentials, then navigate through some drop down menus to select a report. Once the report is selected, a new window pops up where parameters must be adjusted to generate the report. Once the parameters are set, the same pop up window refreshes with the generated report in PDF format and is displayed using Chrome's built in PDF viewer. I was under the impression that passing certain options to the webdriver would disable this PDF viewer and simply download the file, but the PDF viewer is still being displayed and nothing is automatically downloaded. Surely I'm missing something or I wrote something incorrectly. Here's the jist of my code:



    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.chrome.options import Options

    chrome_options = Options()
    chrome_options.add_experimental_option('prefs', {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.plugins_disabled": ["Chrome PDF Viewer"]
    }
    )

    browser = webdriver.Chrome(options = chrome_options)

    driver = webdriver.Chrome()
    driver.get(url)

    #In between here are a bunch of steps here that navigates through drop down menus

    #This step may not be necessary, but I figured I'd include it to address when the pop up window refreshes and displays the report in PDF format through Chrome's PDF viewer
    driver.switch_to.window(driver.window_handles[1])


    So, at this point, Chrome still displays the PDF viewer even though I disabled it earlier. Nothing is downloaded, so I'm wondering if I need to provide another line of code or perhaps something else.



    Using Selenium version 3.141.0, Python 3.6.4, Chrome webdriver 2.45 on Windows 10.










    share|improve this question

























      2












      2








      2








      I tried following previous posts on this topic such as these (post 1, post 2), but I'm still stuck.



      My script has to log into a site using a set of credentials, then navigate through some drop down menus to select a report. Once the report is selected, a new window pops up where parameters must be adjusted to generate the report. Once the parameters are set, the same pop up window refreshes with the generated report in PDF format and is displayed using Chrome's built in PDF viewer. I was under the impression that passing certain options to the webdriver would disable this PDF viewer and simply download the file, but the PDF viewer is still being displayed and nothing is automatically downloaded. Surely I'm missing something or I wrote something incorrectly. Here's the jist of my code:



      from selenium import webdriver
      from selenium.webdriver.common.action_chains import ActionChains
      from selenium.webdriver.chrome.options import Options

      chrome_options = Options()
      chrome_options.add_experimental_option('prefs', {
      "download.default_directory": download_dir,
      "download.prompt_for_download": False,
      "download.directory_upgrade": True,
      "plugins.plugins_disabled": ["Chrome PDF Viewer"]
      }
      )

      browser = webdriver.Chrome(options = chrome_options)

      driver = webdriver.Chrome()
      driver.get(url)

      #In between here are a bunch of steps here that navigates through drop down menus

      #This step may not be necessary, but I figured I'd include it to address when the pop up window refreshes and displays the report in PDF format through Chrome's PDF viewer
      driver.switch_to.window(driver.window_handles[1])


      So, at this point, Chrome still displays the PDF viewer even though I disabled it earlier. Nothing is downloaded, so I'm wondering if I need to provide another line of code or perhaps something else.



      Using Selenium version 3.141.0, Python 3.6.4, Chrome webdriver 2.45 on Windows 10.










      share|improve this question














      I tried following previous posts on this topic such as these (post 1, post 2), but I'm still stuck.



      My script has to log into a site using a set of credentials, then navigate through some drop down menus to select a report. Once the report is selected, a new window pops up where parameters must be adjusted to generate the report. Once the parameters are set, the same pop up window refreshes with the generated report in PDF format and is displayed using Chrome's built in PDF viewer. I was under the impression that passing certain options to the webdriver would disable this PDF viewer and simply download the file, but the PDF viewer is still being displayed and nothing is automatically downloaded. Surely I'm missing something or I wrote something incorrectly. Here's the jist of my code:



      from selenium import webdriver
      from selenium.webdriver.common.action_chains import ActionChains
      from selenium.webdriver.chrome.options import Options

      chrome_options = Options()
      chrome_options.add_experimental_option('prefs', {
      "download.default_directory": download_dir,
      "download.prompt_for_download": False,
      "download.directory_upgrade": True,
      "plugins.plugins_disabled": ["Chrome PDF Viewer"]
      }
      )

      browser = webdriver.Chrome(options = chrome_options)

      driver = webdriver.Chrome()
      driver.get(url)

      #In between here are a bunch of steps here that navigates through drop down menus

      #This step may not be necessary, but I figured I'd include it to address when the pop up window refreshes and displays the report in PDF format through Chrome's PDF viewer
      driver.switch_to.window(driver.window_handles[1])


      So, at this point, Chrome still displays the PDF viewer even though I disabled it earlier. Nothing is downloaded, so I'm wondering if I need to provide another line of code or perhaps something else.



      Using Selenium version 3.141.0, Python 3.6.4, Chrome webdriver 2.45 on Windows 10.







      python selenium selenium-chromedriver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 20:24









      erik7970erik7970

      1218




      1218
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Just replace "plugins.plugins_disabled": ["Chrome PDF Viewer"]



          With:



          "plugins.always_open_pdf_externally": True


          Hope this helps you!






          share|improve this answer
























          • I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

            – erik7970
            Jan 2 at 19:14








          • 1





            Nevermind! Got it to work! Thank you!

            – erik7970
            Jan 2 at 19:36











          • Glad to hear!!!

            – Moshe Slavin
            Jan 2 at 20:04



















          2














          I had a similar problem, which I have solved with the firefox driver in Java.
          Here is my code:



          ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
          ffprofile.setPreference("browser.download.folderList", 2);
          ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
          ffprofile.setPreference("browser.download.dir", "path/to/directory");
          ffprofile.setPreference("plugin.scan.plid.all",false);
          ffprofile.setPreference("plugin.scan.Acrobat","99.0");
          ffprofile.setPreference("pdfjs.disabled",true);


          Maybe for you it is an option to use Firefox and the Java->Python translation should be simple.






          share|improve this answer
























          • this is a good answer for Java in Python is much simpler... ;)

            – Moshe Slavin
            Jan 2 at 10:43











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998690%2fdownloading-a-pdf-using-selenium-chrome-and-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Just replace "plugins.plugins_disabled": ["Chrome PDF Viewer"]



          With:



          "plugins.always_open_pdf_externally": True


          Hope this helps you!






          share|improve this answer
























          • I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

            – erik7970
            Jan 2 at 19:14








          • 1





            Nevermind! Got it to work! Thank you!

            – erik7970
            Jan 2 at 19:36











          • Glad to hear!!!

            – Moshe Slavin
            Jan 2 at 20:04
















          2














          Just replace "plugins.plugins_disabled": ["Chrome PDF Viewer"]



          With:



          "plugins.always_open_pdf_externally": True


          Hope this helps you!






          share|improve this answer
























          • I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

            – erik7970
            Jan 2 at 19:14








          • 1





            Nevermind! Got it to work! Thank you!

            – erik7970
            Jan 2 at 19:36











          • Glad to hear!!!

            – Moshe Slavin
            Jan 2 at 20:04














          2












          2








          2







          Just replace "plugins.plugins_disabled": ["Chrome PDF Viewer"]



          With:



          "plugins.always_open_pdf_externally": True


          Hope this helps you!






          share|improve this answer













          Just replace "plugins.plugins_disabled": ["Chrome PDF Viewer"]



          With:



          "plugins.always_open_pdf_externally": True


          Hope this helps you!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 10:27









          Moshe SlavinMoshe Slavin

          2,41441026




          2,41441026













          • I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

            – erik7970
            Jan 2 at 19:14








          • 1





            Nevermind! Got it to work! Thank you!

            – erik7970
            Jan 2 at 19:36











          • Glad to hear!!!

            – Moshe Slavin
            Jan 2 at 20:04



















          • I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

            – erik7970
            Jan 2 at 19:14








          • 1





            Nevermind! Got it to work! Thank you!

            – erik7970
            Jan 2 at 19:36











          • Glad to hear!!!

            – Moshe Slavin
            Jan 2 at 20:04

















          I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

          – erik7970
          Jan 2 at 19:14







          I tried with the following changes, but it still opens up the PDF in the Chrome PDF viewer: chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )

          – erik7970
          Jan 2 at 19:14






          1




          1





          Nevermind! Got it to work! Thank you!

          – erik7970
          Jan 2 at 19:36





          Nevermind! Got it to work! Thank you!

          – erik7970
          Jan 2 at 19:36













          Glad to hear!!!

          – Moshe Slavin
          Jan 2 at 20:04





          Glad to hear!!!

          – Moshe Slavin
          Jan 2 at 20:04













          2














          I had a similar problem, which I have solved with the firefox driver in Java.
          Here is my code:



          ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
          ffprofile.setPreference("browser.download.folderList", 2);
          ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
          ffprofile.setPreference("browser.download.dir", "path/to/directory");
          ffprofile.setPreference("plugin.scan.plid.all",false);
          ffprofile.setPreference("plugin.scan.Acrobat","99.0");
          ffprofile.setPreference("pdfjs.disabled",true);


          Maybe for you it is an option to use Firefox and the Java->Python translation should be simple.






          share|improve this answer
























          • this is a good answer for Java in Python is much simpler... ;)

            – Moshe Slavin
            Jan 2 at 10:43
















          2














          I had a similar problem, which I have solved with the firefox driver in Java.
          Here is my code:



          ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
          ffprofile.setPreference("browser.download.folderList", 2);
          ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
          ffprofile.setPreference("browser.download.dir", "path/to/directory");
          ffprofile.setPreference("plugin.scan.plid.all",false);
          ffprofile.setPreference("plugin.scan.Acrobat","99.0");
          ffprofile.setPreference("pdfjs.disabled",true);


          Maybe for you it is an option to use Firefox and the Java->Python translation should be simple.






          share|improve this answer
























          • this is a good answer for Java in Python is much simpler... ;)

            – Moshe Slavin
            Jan 2 at 10:43














          2












          2








          2







          I had a similar problem, which I have solved with the firefox driver in Java.
          Here is my code:



          ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
          ffprofile.setPreference("browser.download.folderList", 2);
          ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
          ffprofile.setPreference("browser.download.dir", "path/to/directory");
          ffprofile.setPreference("plugin.scan.plid.all",false);
          ffprofile.setPreference("plugin.scan.Acrobat","99.0");
          ffprofile.setPreference("pdfjs.disabled",true);


          Maybe for you it is an option to use Firefox and the Java->Python translation should be simple.






          share|improve this answer













          I had a similar problem, which I have solved with the firefox driver in Java.
          Here is my code:



          ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
          ffprofile.setPreference("browser.download.folderList", 2);
          ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
          ffprofile.setPreference("browser.download.dir", "path/to/directory");
          ffprofile.setPreference("plugin.scan.plid.all",false);
          ffprofile.setPreference("plugin.scan.Acrobat","99.0");
          ffprofile.setPreference("pdfjs.disabled",true);


          Maybe for you it is an option to use Firefox and the Java->Python translation should be simple.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 10:36









          VolokhVolokh

          1279




          1279













          • this is a good answer for Java in Python is much simpler... ;)

            – Moshe Slavin
            Jan 2 at 10:43



















          • this is a good answer for Java in Python is much simpler... ;)

            – Moshe Slavin
            Jan 2 at 10:43

















          this is a good answer for Java in Python is much simpler... ;)

          – Moshe Slavin
          Jan 2 at 10:43





          this is a good answer for Java in Python is much simpler... ;)

          – Moshe Slavin
          Jan 2 at 10:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998690%2fdownloading-a-pdf-using-selenium-chrome-and-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory