Save a file by clicking on a link

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

···

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

@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

···

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

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

···

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

Joel Pearson wrote in post #1093810:

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

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.

···

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

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

···

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

Joel Pearson wrote in post #1094315:

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

Yes, That was the confirmation I was expecting. Hum,If it can be done
during download,could be great. Now I have to develop such renaming
code.

Thanks for your help.

···

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

Joel Pearson wrote in post #1094315:

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

A serious problem I am having during download of files:

In the webpage there is file : "Christopher Herman BR.bmp"

But after the download I am getting two files namely -

"Christopher Herman BR.bmp" and "Christopher Herman BR.bmp.part" -- this
is the reason original file getting corrupted. How such `.part` file is
coming?

Could you please tell me why so?

The final profile I am using:

···

==========================================================================

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = 'C:\Documents and Settings\rakshiar\My
Documents\userdata\Rubydownloads'
profile['browser.download.folderList'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] =
"application/pdf,image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-windows-bmp,image/tif,image/x-tif,image/tiff,image/x-tiff,application/x-compressed,application/x-zip-compressed,application/zip,multipart/x-zip"
profile['browser.download.manager.showWhenStarting'] = false
driver = Selenium::WebDriver.for :firefox, :profile => profile

==========================================================================

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

Joel Pearson wrote in post #1094622:

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

Can you do me a favor by re-writing it? I am totally confused here.

···

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

Arup Rakshit wrote in post #1094617:

@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

Could you please answer my above said questions?

···

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

Joel Pearson wrote in post #1094713:

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:

OMG! Still not working.

Dir.chdir("C:/Documents and Settings/rakshiar/My
Documents/userdata/Rubydownloads") do

                    until Dir.glob('**/*.part*')[0] !=

                       sleep 0.1
                       #puts "h"

                    end

Any better solution? It seems this couldn't stop the execution of next
line,until the download finished.

I also have seen that,during download of the file - "Christopher Herman
BR.bmp" , two target folders created. The file size is 1.02 MB.

···

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

No prompting, please! :slight_smile:

And there are better ways to test for an empty array.

···

Am 01.02.2013 14:34, schrieb Joel Pearson:

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

--
<https://github.com/stomar/&gt;

You probably mean

   head.empty? == true

but the `== true' part is not necessary:

   study while head.empty?

:wink:

···

Am 01.02.2013 14:50, schrieb Joel Pearson:

head.empty? = true

--
<https://github.com/stomar/&gt;

Arup Rakshit wrote in post #1093462:

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,

Please aboid 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.

···

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

Arup Rakshit wrote in post #1093512:

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

Attachments:
http://www.ruby-forum.com/attachment/8079/download.png

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

Arup Rakshit 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
____________

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

    difference = [1,2] - here why you took size 2 of array?

So that a variable called difference exists for the next line, and is
not 1.

    until difference.size == 1 - why 1?

Because then there will be a single file, which is the one you've just
downloaded. More than one is venturing into the unknown :slight_smile:

      difference = Dir.entries(@download_directory) - @current_downloads
in the above line what are we doing?

The current files minus the previous files = the new file.

···

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

I did not follow this thread, but I am pretty sure that
the condition you are using does not test correctly what
you want to find out.

Consider that Dir.glob returns an array of filename strings,
so that your condition *always* is true. (The first element
will never be an empty array).

A tiny change to the code will fix this...

Furthermore, assuming that you want to sleep as long as
the partial file *does* exists, you would also have to
use `while' instead of `until' (or invert the condition).

General tip: test the code snippet in isolation (using irb),
with files you create or delete manually.

···

Am 01.02.2013 11:49, schrieb Arup Rakshit:

OMG! Still not working.

Dir.chdir("C:/Documents and Settings/rakshiar/My
Documents/userdata/Rubydownloads") do

                     until Dir.glob('**/*.part*')[0] !=

                        sleep 0.1
                        #puts "h"

                     end

Any better solution? It seems this couldn't stop the execution of next
line,until the download finished.

--
<https://github.com/stomar/&gt;

Joel Pearson wrote in post #1094173:

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

···

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

Joel Pearson wrote in post #1094173:

···

Arup Rakshit 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}"

=================================
I think below should work also:

Dir.chdir("C:/Documents and Settings/peter/My
Documents/userdata/Rubydownloads") do

  until Dir.glob('**/*.part*')[0] !=

   sleep 0.1

  end

But also confused - is this the optimized one? Or could it be more
better with a more bit of engineering?
end

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

unknown wrote in post #1094748:

                     end

Any better solution? It seems this couldn't stop the execution of next
line,until the download finished.

I did not follow this thread, but I am pretty sure that
the condition you are using does not test correctly what
you want to find out.

Consider that Dir.glob returns an array of filename strings,
so that your condition *always* is true. (The first element
will never be an empty array).

A tiny change to the code will fix this...

Furthermore, assuming that you want to sleep as long as
the partial file *does* exists, you would also have to
use `while' instead of `until' (or invert the condition).

General tip: test the code snippet in isolation (using irb),
with files you create or delete manually.

Yes! Exactly this I have followed and made the code working: One change
I did-

until Dir.glob('**/*.part*')==

   sleep 0.1
   #puts "h"

end

Now all are set, for GOD sake:) Thanks to all of you! You people are
really love Ruby- which is proved by your continuous suggestions. It
seems I started to fall in love with "Ruby" :stuck_out_tongue:

···

Am 01.02.2013 11:49, schrieb Arup Rakshit:

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