Selenium-Phantomjs download csv
Asked Answered
H

1

7

I am trying trying to download (save to disk) a CSV file using PhantomJS, from a dialogue box. Using firefox profile, this would be fairly simple by setting the browser profile properties. Any suggestions how could excel file be downloaded in phantomjs?

This is how it would be done using firefox driver:

profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.dir",self.opts['output_dir'])
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/octet-stream,application/msexcel'))

I am using Phantomjs driver:

webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true','--local-storage-path=/tmp'])

and looking for a way to set properties which can override save to disk and set MIME type of the data. Currently without having the properties set, PhantomJS driver, does not download the file.

I have read links about avoiding dialog box etc but in this case, it is needed.

Hultgren answered 18/2, 2014 at 20:8 Comment(3)
What happens when you try to download the file this way?Calculate
I have provided an example of how it would work in Firefox. I am looking for a way to set properties for PhantomjS driver, which can override save to disk and set MIME type of the data. Currently without having the properties set, PhantomJS driver, does not download the file.Hultgren
There is a fork of phantomjs that apparently provides download support, see this group messageEnthusiast
V
0

I was recently struggling with a similar issue. However I ended up switching the web driver because it offers the ability to access network traffic relatively easily. This means that if a file is not directly on the page and is rather transferred in you cannot see it in phantom. There are a few people working on work arounds but I found that most of my files were being transferred and thus it was easier for me to gather network traffic with web driver + firebug + net export.

However in phantomjs very hacky way to do this would be something like this:

phantomjs.exe file_to_run.js > my_log.txt

Where you simply save the console contents to file. However you are likely to get errors and other messages in your file. You could clean it since you're only looking for cdv.

From my understanding PhantomJS is limited as the developer has a very specific idea for how it should be. For example they discontinued supporting flash. There is not a easy native way of downloading and saving files like the way you can in firefox. You could launch another web browser and download through that. However I think the easiest way to do this is to use CasperJS which plays nicely with PhantomJS.

A good example of using casperJS to download files can be found here: casperjs download csv file

I believe that the major issue with using casper however is that large files are not well supported. Is there a specific reason you are preferring to use a headless browser?

Villada answered 13/6, 2014 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.