0

I was trying to test with Selenium, but can not download a pdf, pdfs keep opening.

See my other post: RUBY: Selenium webdriver, setup to download pdf files instead of opening them

It was advised to try Watir, so I did, and I get the same result. Here is my Watir setup. Please advise on how to fix this issue.

require 'watir'
require 'pry'

prefs = {
  download: {
    prompt_for_download: false,
    default_directory: '/Users/ar/pdf_downloads'
  }
}

browser = Watir::Browser.new :chrome, options: {prefs: prefs}

# Goto Login Page (file)
url="file:///Users/ar/info.html"
browser.goto url

browser.button(id: 'formsubmit').click

sleep 5
# Goto info
info_url = 'https://webapp.domain.com/info'
browser.goto info_url

sleep 5
elements = browser.elements(css: "#ar-pdfreport a")
link = elements.first.attribute("href")
browser.goto link

1 Answer 1

0

There is a bug in Selenium-WebDriver v3.142.7 where using symbols for the prefs generates the wrong result - eg does not set the download directory. See https://github.com/SeleniumHQ/selenium/issues/7917 for more details.

Switch the symbols to Strings:

prefs = {
  download: {
    'prompt_for_download' => false,
    'default_directory' => '/Users/ar/pdf_downloads'
  },
  plugins: {
    'always_open_pdf_externally' => true
  }  
}   
4
  • It's being set now, but I also need to change the setting for Chrome - settings/content/pdfDocuments. How do I change the above to do this? I can't find how. Thank you!
    – Lefty
    Feb 17, 2021 at 21:47
  • The above settings will work perfectly If the file were to be downloaded, that is, If we can download the file manually then the above settings will work. But in the case of PDF, when you click the link, it will open in the viewer, so it wouldn't even fall into the category of downloading the file. So the above syntax wouldn't work I guess. Feb 18, 2021 at 4:44
  • Something called plugins_always_open_externally needs to be set. Feb 18, 2021 at 5:23
  • As @ArundeepChohan mentioned, you'll need to set an extra preference for that. See updated answer.
    – Justin Ko
    Feb 18, 2021 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.