Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I've been grappling with using PhantomJS/Selenium/python-selenium to download a file to the filesystem. I'm able to easily navigate through the DOM and click, hover, etc. Downloading a file is, however, proving to be quite troublesome. I've tried a headless approach with Firefox and pyvirtualdisplay but that wasn't working well either and was unbelievably slow. I know That CasperJS allows for file downloads. Does anyone know how to integrate CasperJS with Python or how to utilize PhantomJS to download files? Much appreciated.

1 Answer

0 votes
by (62.9k points)

Though downloading files through PhantomJS might be difficult, but we can use PhantomJS to get us to download links and fetch all needed cookies such as CSRF(Cross-site-request-forgery) tokens and so on. And then we can use requests to download it like this:

import requests

from selenium import webdriver

driver = webdriver.PhantomJS()


download_link = driver.find_element_by_id('download_link')

session = requests.Session()

cookies = driver.get_cookies()

for cookie in cookies: 

    session.cookies.set(cookie['name'], cookie['value'])

response = session.get(download_link)

And now in response.content actual file content will appear. We can then write in the file using open method or perform other tasks which we want to(like writing) by automation testing.

Browse Categories