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
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.
#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
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.
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
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.
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.
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
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.