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
Related
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")
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.
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()
I am able to download file in normal chrome mode. where as, i am not able to see the download happening in headless chrome using selenium python.
I hope it is not saving the file downloaded
Tried with solutions provided by many users in internet but none of them works
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
prefs = {'download.default_directory' :'/Users/nrpss/Downloads'}
options.add_experimental_option('prefs', prefs)
download_path = '/Users/nrpss/Downloads'
browser = webdriver.Chrome('chromedriver.exe', options=options)
browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_path}}
command_result = browser.execute("send_command", params)
print ("Headless Chrome Initiated")
### Below is ID for the Download link on webpage
browser.find_element_by_id('downloadExportLink').click()
time.sleep(50)
def download_completed():
for i in os.listdir('/Users/nrpss/Downloads'):
if ".crdownload" in i:
time.sleep(1)
download_completed()
Expected result: File should be downloaded and saved in downloads folder.
Try adding download.prompt_for_download = False and download.directory_upgrade = True you car set safebrowsing_for_trusted_sources_enabled to False as well as safebrowsing.enabled.
try changing your prefs to:
prefs = {'download.default_directory' :'/Users/nrpss/Downloads',
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
}
options.add_experimental_option('prefs', prefs)
Hope this helps you!
to enable headless downloads in Python:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = Chrome(options=options)
params = {'behavior': 'allow', 'downloadPath': '/path/for/download'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2);
fp.set_preference("browser.download.manager.showWhenStarting", False);
fp.set_preference("browser.download.dir", self.download_dir);
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
self.browser = webdriver.Remote("http://192.168.1.242:4444/wd/hub",
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
browser_profile=fp
)
the above code does not respect the profile specified.
BUT the code below works as expected:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2);
fp.set_preference("browser.download.manager.showWhenStarting", False);
fp.set_preference("browser.download.dir", self.download_dir);
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
self.browser = webdriver.Firefox(fp)
In the seleniums documentation page http://seleniumhq.org/docs/04_webdriver_advanced.html#remotewebdriver
has the following example:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
# set something on the profile...
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX, browser_profile=fp)
which is the same as the code in my example.
Also when i start the selenium server with -firefoxProfileTemplate it seems to ignore the profile's settings
java -jar ./selenium-server-standalone-2.25.0.jar -firefoxProfileTemplate /home/xubuntu/.mozilla/firefox/fdui6lsj.crawler/
EDIT:
I also want to mention that if I load the profile from the file:
fp = webdriver.FirefoxProfile('/home/xubuntu/.mozilla/firefox/fdui6lsj.crawler/')
self.browser = webdriver.Remote("http://192.168.1.242:4444/wd/hub",
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
browser_profile=fp
)
the profile is loaded but it takes a lot of time.
Can someone tell me what is wrong?
Try calling update_preferences() at the end. That should force the writing of the config file:
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
fp.update_preferences()