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??

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

Try this:

@profile['browser.download.manager.showWhenStarting'] = false

···

--
Posted via http://www.ruby-forum.com/.

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

Or

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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"

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

During the download, no. After the download, yes.

···

--
Posted via http://www.ruby-forum.com/.

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

  def wait_for_download

    #Find the newest file
    difference = [1,2] - here why you took size 2 of array?
    until difference.size == 1 - why 1?
      difference = Dir.entries(@download_directory) - @current_downloads -- in

the above line what are we doing?

···

      sleep 0.1
    end

--
Posted via http://www.ruby-forum.com/\.

difference = Dir.entries(@download_directory) - @current_downloads

If you subtract one array from another you get the difference. In this
case, the difference is the most recent file.

"BR.bmp.part" is the result of a temporary file being created during the
download, this is one of the reasons I said the download waiting method
needs a bit of rewriting. It should continue to look until there is only
one file difference, but a "part" file might mess that up because the
difference is then two files. You'd just have to adapt the code to suit
that scenario.
It isn't something I've had to deal with before because all the file
downloads I've done have been tiny.

···

--
Posted via http://www.ruby-forum.com/.

difference = Dir.entries('C:/').reject {|f| f =~ /.part\z/ } -
@current_downloads

···

--
Posted via http://www.ruby-forum.com/.

Almost everything can be done differently. You just have to tailor the
code to suit your way of thinking and your specific task.
As for the "best" way to do something, that will always depend on your
objectives - Do you want it... easy to make, easy to use, easy to
expand, immutable, versatile, limited, fast to run, intelligent enough
to correct user errors, etc.

This is why we "play" with code. It's the way you'll discover new
things, sometimes approaches no one else has ever thought of :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Try increasing the interval (to 1 or more for testing purposes). Perhaps
the ".part" file and the destination file are created more than 0.1
second apart from each other and it isn't picking them up
simultaneously. That way you could use the original code, because it
should continue to wait until the two files become only 1 file.

···

--
Posted via http://www.ruby-forum.com/.

sleep(0.1) while Dir.glob('(Path)/**/*.part*')[0] != nil

···

--
Posted via http://www.ruby-forum.com/.

head.empty? = true

···

--
Posted via http://www.ruby-forum.com/.

I don't write pseudo-code carefully :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Joel Pearson 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

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson 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,

···

--
Posted via http://www.ruby-forum.com/\.