Using Selenium with Python and PhantomJS to download file to filesystem

后端 未结 4 1806
感动是毒
感动是毒 2020-12-05 02:50

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

4条回答
  •  温柔的废话
    2020-12-05 03:44

    Despite this question is quite old, downloading files through PhantomJS is still a problem. But we can use PhantomJS to get download link and fetch all needed cookies such as csrf tokens and so on. And then we can use requests to download it actually:

    import requests
    from selenium import webdriver
    
    driver = webdriver.PhantomJS()
    driver.get('page_with_download_link')
    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 should appear. We can next write it with open or do whatever we want.

提交回复
热议问题