简体   繁体   中英

Watir - How to save a pdf file when the file opens in html

When I click on a link, the pdf opens in the browser. My test requires me to save the pdf onto the desktop.

I was able to locate the new window where the pdf is opened but unable to save. As to save the pdf, i have to either hover the mouse on the bottom to get the pdf icon or I will have to go to file-> Save as to save the pdf.

here is sample code of how you can do it in firefox

download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"

b = Watir::Browser.new :firefox, :profile => profile

and Chrome

download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if  Selenium::WebDriver::Platform.windows?

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory

b = Watir::Browser.new :chrome, :profile => profile

with ie you will probably have to do something like this:

f = open('sample.pdf')
begin
    http.request_get('/sample.pdf') do |resp|
        resp.read_body do |segment|
            f.write(segment)
        end
    end
ensure
    f.close()
end

Code Ref

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM