Auto Download a zip file from firefox using selenium - python

I had a selenium script to download a file from a website and i have used these preferences to get out of getting the pop up on the final download of the file, still, i get this pop up on final end.
I don't need this pop-up and my zip file should auto download please help me with a solution.
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip')
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.closeWhenDone", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
profile.set_preference("pdfjs.disabled", True)
profile.update_preferences()
Pop up Image

It's because of the "application/zip". Try this instead:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.preferences.instantApply",True)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream") #I realy dont know why application/zip doesnt work here
profile.set_preference("browser.download.dir", 'F:\\drivers')
driver = webdriver.Firefox( firefox_profile = profile,executable_path='F:\\drivers\\geckodriver')
driver.get('https://github.com/mozilla/geckodriver/releases')
driver.find_element_by_xpath('//a[#href="/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-win64.zip"]').click()

Related

Python Selenium problem with downloading file, preferences its not ok

I've got problem. On the page after clicked button download, the window os with"Open and exported documents" shows. On this window I see two radio buttons: 1. Open file 2. Save file. Default is checked "Open file" but I would like to check "Save file" and next click "OK"
I've got this code, but it doesn't work correctly.
Where is the mistake?
I've wiritten example_path in url. It's unreal path.
import os
from selenium import webdriver
url = "https://example_path"
profile = webdriver.FirefoxProfile()
driver_csv = webdriver.Firefox(firefox_profile=profile, executable_path= r"Desktop/programs/folder/geckodriver.exe")
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.dir", os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.openFile', "application/csv, text/csv, text/comma-separated-values")
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/csv, text/csv, text/comma-separated-values")
profile.set_preference("plugin.disable_full_page_plugin_for_types", "application/csv, text/csv, text/comma-separated-values")
profile.set_preference("browser.download.useDownloadDir", True)
# profile.set_preference("pdfjs.disabled", False)
profile.set_preference("browser.tabs.warnOnClose", False)
driver.get(url)

Python Selenium, Firefox driver. Dismiss Open/Save file popup

this is my setup for the driver:
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
#binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
fp = (r'C:\Users\user\AppData\Roaming\Mozilla\Firefox\Profiles\fdjhsjfhd.default')
opts = Options()
opts.profile = fp
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2) # 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", download_directory)
fp.set_preference('browser.helperApps.neverAsk.saveToDisk','image/jpeg,image/png,excel,text/plain,csv/text,application/octet-stream')
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
But still I get the pop, as per attached.
What is wrong?
Thanks
Found, credits to #cruisepandey:
those are the essential for this purpose:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.dir", download_directory)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.useDownloadDir", True)
For one of my project I am using the same version of Selenium and below is my configuration:
profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
profile.set_preference("pdfjs.disabled", True);
driver = webdriver.Firefox(firefox_profile = profile, executable_path = "D:\geckodriver.exe")
Import:
from selenium.webdriver import FirefoxProfile

Disable download pop up Firefox Selenium

I am very new to Selenium and I am typing up my first script to download a .csv file from a webpage.
Problem is, when I click the button to download the .csv file a download window pops up. How do I automatically save the file to a folder?
I tried a variety of profiles but I can't seem to get them to work.
I am using the below configuration in one of my project
You can possibly have this firefox profile, In Python you could this :
profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
profile.set_preference("pdfjs.disabled", True);
profile.set_preference("browser.download.dir", "C:\\Users\\***\\****\\Desktop\\Automation")
driver = webdriver.Firefox(firefox_profile = profile, executable_path = "Full file path to gecko driver.exe")

What is the equivalent selenium code for the chrome?(Selenium)

I have written a simple selenium code that downloads files into the custom directory. The code is
for firefox:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList",2)
# 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.download.dir",dest_dir1)
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="geckodriver.exe")
Now, I want to create a similar type of script for chrome. I just want the chrome script to download into the dest_dir1. I don't see any options like webdriver.ChromeProfile similar to webdriver.FirefoxProfile().
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": "YOUR DOWNLOAD DIRECTORY",
"download.directory_upgrade": True,
"download.manager.showWhenStarting": False,
"download.manager.useWindow": False,
"helperApps.alwaysAsk.force":False,
"download.manager.showAlertOnComplete": False}
}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options, path="EXECUTABLE CHROME DRIVER PATH")
I have added few tags hope you understand how it works and build your own preference option.

MacOS - Python Selenium Geckodriver - Set Preference Download Directory is not accepting relative folder path

Down below where I have browser.download.dir I have to use the absolute path for it to download to the directory I need it to.
But if I change it to a relative path, it downloads it to the default directory (Downloads)
I am running the script from the /Users/myname/magic/script directory
I have tried
./sites/pdd/notes and sites/pdd/notes
I have also tried
getdir = os.path.dirname(__file__)
profile.set_preference('browser.download.dir', getdir+'/notes')
expected_download = 'sites/pdd/downloads/'
mime_types = "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml"
options = Options()
options.headless = True
# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/Users/myname/magic/script/sites/pdd/notes')
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.closeWhenDone", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
options.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types)
options.set_preference("plugin.disable_full_page_plugin_for_types", mime_types)
options.set_preference("pdfjs.disabled", True)
driver = webdriver.Firefox(firefox_profile=profile, firefox_options=options, executable_path=r'mac/geckodriver')
Is there a way to get around having to use the absolute path?

Categories

Resources