Selenium Firefox profile preferences download dialogue box - python

I am trying to automatically log into several websites and download reports.
In my profile preferences, I have set the following:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", folder1)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf,text/html")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf,text/html")
For all the websites, except for 1, the files download without the dialog box showing.
However, one of them always shows the dialog box.
I am thinking it is due to the filename being called "Download.CSV", with the csv file extension in capitals...but I'm not convinced.
All the other files that successfully downloaded from the other websites were in csv file with a lower case csv file extension. This is the only difference I can think of.
Am I missing something?

For anyone who will come across this issue, I solved it.
Although the file extension was "CSV", the MIME type from the server was listed as "application/octet-stream".
Adding this to my Firefox profile preferences fixed the issue.

Related

Changing The Download Path After Opening Selenium Firefox Browser

I have a collection of files that i download with a loop. I want to folder these files separately. Before the browser opens, I can change the default download path with the options parameter. However, I want to folder the files I will download separately after the browser is opened.
Can you please help?
`
optns = Options()
optns.set_preference("browser.download.folderList", 2)
optns.set_preference("browser.download.manager.showWhenStarting", False)
optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files")
optns.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
browser = webdriver.Firefox(options=optns)
???
optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files")
`
You can not do that.
Once driver object instance is created by browser = webdriver.Firefox(options=optns) you can no more change it settings.
The only ways you can do here are:
re-define optns.set_preference("browser.download.dir", "/Users/emrevolkanucar/Work/Ekap Py/Files") and then create a new driver instance. I'm not sure you really want to do that.
After downloading files to defined (or default) downloading folder you can programmatically move those file to separate target folders. This approach seems better.

Selenium Python Edge disable `open office files in browser` setting

Python: 3.9.9
Selenium: 4.1.5
Edge: 101.0.1210.39 (X64) driver link
I am trying to automate downloading excel file from a website, but due to Edge's default setting of open office files in browser set to True, on pressing download button with selenium it redirects to Edge file viewer instead of downloading it.
Since I want to automate the process I don't want to manually go to settings and disable it every time.
Any work arounds will be appreciated too...
Thank you!
This is what worked for me:
from pathlib import Path
from selenium import webdriver
if Path('..\msedgedriver.exe').exists():
driver = webdriver.Edge('..\msedgedriver.exe')
# Settings
driver.get('edge://settings/downloads')
toggle = driver.execute_script('''
return document.querySelector(' input[aria-label="Open Office files in
the browser"]');
''')
toggle.click()
# continue...
similarly you can change any setting as per required.

Automating PDF download with Python Selenium Chromedriver

I am using Chrome/Chromedriver version: 93.0.4577 and Selenium version: 3.141.0 on Win 10. I'm trying to get Selenium to download PDF's without opening the file prompt.
I'm working with a work site unfortunately so I'm unable to share it but I've tried the libraries: urllib, request, as well as adjusting the chrome Options (which I'd like to use):
chrome_profile = webdriver.ChromeOptions()
profile = {"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}],
"download.default_directory": "C:\\PDFDownload\\PDFs",
"download.prompt_for_download": False,
"download.directory_upgrade": True}
chrome_profile.add_experimental_option("prefs", profile)
driver = webdriver.Chrome(executable_path="C:\\Selenium\\chromedriver.exe",
chrome_options=chrome_profile)
I've also tried switching the chrome PDF site settings from 'Open PDFs in Chrome' to 'Download PDFs' when visiting them. However, everything still prompts me to save the file manually.
I feel like the chrome Options should be the winning ticket but it doesn't seem to have worked for me. Is there something I need to change/add in order to get past the pop up prompt?
I appreciate any advice, thank you!
You can try the following:
First get the 'href' or download link of the PDF you're looking for. There must be some button or link which you might want to catch.
use self.driver.get(the_link_you_extracted_in_step_1) and the PDF should be automatically downloaded

Problem with browser.helperApps.neverAsk.saveToDisk in selenium

I want to automatically download a .ics file (a calendar file) from a website using selenium with python. The goal is to disable the pop-up window that firefox open when you download a file. To do this, I use the following code :
#I set my preferences
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)#not using default folder for downloading
profile.set_preference("browser.download.manager.showWhenStarting", False)#dont show downloading process
profile.set_preference("browser.download.dir", 'C:/Users/UserName/Documents/rpi/some folder')#set the directory for download
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/calendar')#tell it to automaticaly download a file
#using the profile to access firefox
browser = webdriver.Firefox(executable_path='geckodriver',firefox_profile=profile)
I first thought that it was a MIME type problem in the line browser.helperApps.neverAsk.saveToDisk, but after changing the MIME type it's still not working.
From here I have no idea of what is going wrong because my MIME type seems right according to all internet resources I have been able to find.
Perhaps it is a problem of settings or something I haven't noticed...
Anyway thank you for reading this, ask me if you need a bit more code.
Hello from what I understand, downloading the file prompts a alert box asking if you want to download it, have you tried,
browser.switchtoalert.accept?
Sorry i write in VBA, but you should get the idea,
Thanks
So I did find a solution by avoiding the problem : i created a new firefox profile > launched firefox with this profile (all of this is done in about:profiles) > went to the web site from wich I wanted to download the file > downloaded the file and checked the "always do this with this type of file" box > launch my programm with this custom profile using the line :
profile= webdriver.FirefoxProfile("C:/Users/user/AppData/Roaming/Mozilla/Firefox/Profiles/bqpa3bzv.nameofprofile")
This seems to work fine and might be an efficient way of doing this.
Well done for figuring it out, I know the
.switchtoalert.accept works in chromedriver
Let me know if you want any help with anything 👍👍

Selenium in Python to download file: even after setting Firefox Profile the Download Window opens

I am trying to use Selenium in Python to download a file from a website. In order to do that, I have read that I need to change the settings in my Firefox Profile to avoid opening the download dialogue window. I provided sample code below. This code works absolutely great at home, but it does not function properly with my work PC. I am suspecting that somehow Python can not change the settings of the firefox profile, even though the code below does not throw an error but rather works fine and in the end opens the download dialogue window.
from selenium import webdriver
import os
profile = webdriver.FirefoxProfile("C:\\Users\\Ric\\Documents\\Python Scripts\\FirefoxProfileCopies\\ric.copy")
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',('application/vnd.ms-excel'))
browser = webdriver.Firefox(profile)
browser.get("http://www.sample-videos.com/download-sample-xls.php")
elem1 = browser.find_element_by_css_selector(".push-form > table:nth-child(2) > tbody:nth-child(2) > tr:nth-child(4) > td:nth-child(4) > a:nth-child(1)")
elem1.click()
This code works perfectly with my Firefox and its profile at home, but not with my computer at work. Does anybody know why this might be? Thank you in advance.
EDIT
I tried to add all the MIMEtypes from the Microsoft webpage, but still, the download manager window opens. When stopping the code to execute before opening the download link and trying to look at the settings for the used firefox profile with about:configthe following values are displayed:
So, after a lot of trying, I figured to look at the firefox settings in Firefox again, since it worked with an empty profile. I managed to resolve my issue and finally have the download window disappear by going to firefox, settings and changing the settings for applications:
Then, when opening this menu, search for excel and change the values from "asking every time" to "save file/download file". Sorry if these entries in the list differ from the actual ones in firefox but my Firefox is in German. After doing this, my issue was resolved. I hope it resolves somebody else :) and thanks to anderson.

Categories

Resources