Search code examples
pythonpython-3.xpython-requestszippython-zip

How to bypass the alert window while downloading a zipfile?


If I open the link: https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip

This link shows the window and I need to press the OK button and it downloads the file.

The alert is not from the browser, it is from the page itself.

But When I tried the script:

from io import BytesIO
from zipfile import ZipFile
import requests


def get_zip(file_url):
    url = requests.get(file_url)
    zipfile = ZipFile(BytesIO(url.content))
    zipfile.extractall("")

file_link ='https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip'

get_zip(file_link)

This throws the error:

zipfile.BadZipFile: File is not a zip file

And when I tried:

import requests

url = r'https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip'
output = r'downloadedfile.zip'

r = requests.get(url)
with open(output, 'wb') as f:
    f.write(r.content)

This downloads the content of the page showing the OK button. Any idea how to solve this:, the link downloads the zip file.


Solution

  • I believe you are accepting answer using selenium, Here's what you can do using selenium :

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    profile = webdriver.FirefoxProfile()
    
    profile.set_preference("browser.download.folderList",1)
    # 0 for desktop
    # 1 for default download folder
    # 2 for specific folder 
    # You can specify directory by using profile.set_preference("browser.download.dir","<>")
    
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
    profile.set_preference("browser.helperApps.alwaysAsk.force", False);
    
    # If you don't have some download manager then you can remove these
    profile.set_preference("browser.download.manager.showWhenStarting",False)
    profile.set_preference("browser.download.manager.useWindow", False);
    profile.set_preference("browser.download.manager.focusWhenStarting", False);
    profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
    profile.set_preference("browser.download.manager.showAlertOnComplete", False);
    
    driver=webdriver.Firefox(firefox_profile=profile,executable_path="<>")
    
    driver.get("https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip")
    driver.find_element_by_id("butAgree").click()
    

    Here we are setting some profiles to disable pop out, download dialog.

    It is working perfectly fine in latest version of Firefox and 3.141.0 version of selenium