Save a file by clicking on a link

I clicked a link to download a file using ruby, now I see the open-save
window, I want to save that file in the present directory as file.pdf.

I have no clue how to select component in that window?
change the filename, then check save… not open, and then press ok??

I’ll assume that you’re referring to webdriver and a link within a
browser.
My code is written for Firefox, so you’ll need to modify it for
whichever browser you’re using, but it might give you an idea.

Code:

@profile = Selenium::WebDriver::Firefox::Profile.from_name ‘default’
#Use new if you want a fresh profile rather than your normal one

@profile[‘browser.download.folderList’] = 2 # custom location setting
@profile[‘browser.download.dir’] = @download_directory
@profile[‘browser.helperApps.neverAsk.saveToDisk’] = “application/pdf,
application/octet-stream, text/xml” #Add the appropriate MIME type here
if you have problems

#Open firefox with your modified profile
@@driver = Watir::Browser.new :firefox, :profile => @profile

Joel P. wrote in post #1073000:

I’ll assume that you’re referring to webdriver and a link within a
browser.
My code is written for Firefox, so you’ll need to modify it for
whichever browser you’re using, but it might give you an idea.

Code:

@profile = Selenium::WebDriver::Firefox::Profile.from_name ‘default’
What if I am using IE? or Chrome?
I saw this for chrome but could not find for IE.
#Use new if you want a fresh profile rather than your normal one

@profile[‘browser.download.folderList’] = 2 # custom location setting
@profile[‘browser.download.dir’] = @download_directory
@profile[‘browser.helperApps.neverAsk.saveToDisk’] = “application/pdf,
What will be the name of the downloaded file?
Is there any way to specify/sustomize the name of the downloaded file?
For example the name of the file I want will be id+”.pdf", id is a
variable.
application/octet-stream, text/xml" #Add the appropriate MIME type here
if you have problems

#Open firefox with your modified profile
@@driver = Watir::Browser.new :firefox, :profile => @profile

Joel P. wrote in post #1073016:

You’d probably write a separate function to determine the most recent
file, wait for its size to stop changing, and then rename it.

Does latest version of “selenium-webdriver” would support such
autodownload functionality. To achieve this what-else gems we need to
download, exepect ‘selenium-webdriver’.

Your quick help will be appreciated always. I am currently now
developing such a script.

Thanks,

You’d probably write a separate function to determine the most recent
file, wait for its size to stop changing, and then rename it.

Arup R. wrote in post #1093462:

Joel P. wrote in post #1073016:

You’d probably write a separate function to determine the most recent
file, wait for its size to stop changing, and then rename it.

Does latest version of “selenium-webdriver” would support such
autodownload functionality. To achieve this what-else gems we need to
download, exepect ‘selenium-webdriver’.

Your quick help will be appreciated always. I am currently now
developing such a script.

Thanks,

Please avoid my last comment. Thank you very much to share this. But
after downloading the window is not closed- talking about the
“Downloadable” window.

How to close that- please advice me. As I need to download 600 files
from the browser with that script.

Try this:

@profile[‘browser.download.manager.showWhenStarting’] = false

Joel P. wrote in post #1093474:

Try this:

@profile[‘browser.download.manager.showWhenStarting’] = false

Okay! Thanks. You are awesome. Okay with “selenium-webdriver” is it
possible to close some pop-up browser which are unwanted, using their
‘titles’ or anything else?

Thanks

while @driver.windows.count > 1
@driver.windows.last.close
end

Or

@driver.windows.last.use do
@driver.window.close
end

My first example closes all except the main window. My second example,
as you quoted, will only close the most recent window.

Joel P. wrote in post #1093508:

My first example closes all except the main window. My second example,
as you quoted, will only close the most recent window.

Perfect! No more questions, all understood! Thank you very much :slight_smile:

~~Arup

@driver.windows.last.use do
@driver.window.close
end

With your above code,if three pop-up windows generated for any actions
from the main window. Then how many will be closed by the above loop?

Again you are really awesome! Really appreciated,lot’s to learn from
you.

Thanks

Arup R. wrote in post #1093512:

Joel P. wrote in post #1093508:

My first example closes all except the main window. My second example,
as you quoted, will only close the most recent window.

Hi Joel,

One issue here is - I have set up the profile as below:

#Automatically download files to a given folder profile settings
#---------------------------------------------------------------
profile = Selenium::WebDriver::Firefox::Profile.new
profile[‘browser.download.dir’] =
“D:\Rubyscript\Download\TaxDocumentDownload”
profile[‘browser.download.folderList’] = 2
profile[‘browser.helperApps.neverAsk.saveToDisk’] =
“application/pdf,application/jpg,application/gif,application/bmp,application/tif”
profile[‘browser.download.manager.showWhenStarting’] = false
driver = Selenium::WebDriver.for :firefox, :profile => profile

But except .pdf files,none of them getting downloaded which are
mentioned in line number 4.Find the attached screenshot for the same.

please help me here,is there any correction needed for the settings?

The most likely culprit is the MIME type. Check out this link:

http://superuser.com/questions/284497/determine-the-mime-type-when-downloading-a-file

With the help of the below link - I saved my life this time:

http://www.webmaster-toolkit.com/mime-types.shtml

profile[‘browser.helperApps.neverAsk.saveToDisk’] =
“application/pdf,image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-windows-bmp,image/tif,image/x-tiff”

Joel P. wrote in post #1093810:

The most likely culprit is the MIME type. Check out this link:

http://superuser.com/questions/284497/determine-the-mime-type-when-downloading-a-file

I just setup as below:

profile[‘browser.helperApps.neverAsk.saveToDisk’] =
“application/pdf,image/jpg,image/gif,image/bmp,image/tif”

still the same issues. please help me. you can be my life saver.

I meant go look up the MIME type of the file you’re having trouble with,
then add that specific one. It’ll help you understand what you’re
dealing with, rather than taking a scattershot approach.

Joel P. wrote in post #1093847:

I meant go look up the MIME type of the file you’re having trouble with,
then add that specific one. It’ll help you understand what you’re
dealing with, rather than taking a scattershot approach.

Is it possible to pause the next set of statements until the download is
not being finished?

Thanks

Arup R. wrote in post #1094152:

Is it possible to pause the next set of statements until the download is
not being finished?

I need to rewrite this at some point, it was a quick-fix and doesn’t
currently have failsafes. Anyway, this is what I’m using:


@download_directory = “#{Dir.pwd}/RubyDownloads”
@current_downloads = Dir.entries( @download_directory )

def wait_for_download

#Find the newest file
difference = [1,2]
until difference.size == 1
  difference = Dir.entries(@download_directory) - @current_downloads
  sleep 0.1
end
file_name = difference.first
puts "Found new file: #{file_name}"

new_size = 0
current_size = -1

#Wait for file size to stop changing
until current_size == new_size
  current_size = File.size("#{@download_directory}\\#{file_name}")
  #puts "current file size: #{current_size}"
  sleep 0.1
  new_size = File.size("#{@download_directory}\\#{file_name}")
  #puts "new file size: #{new_size}"
  sleep 0.1
end

end


Joel P. wrote in post #1094173:

Arup R. wrote in post #1094152:

Is it possible to pause the next set of statements until the download is
not being finished?

I need to rewrite this at some point, it was a quick-fix and doesn’t
currently have failsafes. Anyway, this is what I’m using:


@download_directory = “#{Dir.pwd}/RubyDownloads”
@current_downloads = Dir.entries( @download_directory )

def wait_for_download

#Find the newest file
difference = [1,2]
until difference.size == 1
  difference = Dir.entries(@download_directory) - @current_downloads
  sleep 0.1
end

Can we rename the files during their download?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs