Profile preferences doesn't apply in Selenium Python - python

Here is my code. I try to download file using selenium so I want to disable pop up save window but all I've tried didn't work for me. Save window keep showing up. I use Mozilla Firefox and Windows 10.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "./")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "attachment/csv")
binary = '/Users/User/Documents/Geckodriver/geckodriver.exe'
browser = webdriver.Firefox(executable_path = binary, firefox_profile = profile)
If it is important I use Firefox as my regular browser maybe it can somehow affect geckodriver.
I add update line before binary line and it didn't help me.
profile.update_preferences()
I found all these preferences in about:config and they all actually have been applied:
example
Actually I try to download mp3 file if this matters

Once you set all the preferences through profile.set_preference() lines, finally you need to update the preferences using the line:
profile.update_preferences()
So effectively, your code block will be:
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "attachment/csv")
profile.update_preferences()
binary = '/Users/User/Documents/Geckodriver/geckodriver.exe'
browser = webdriver.Firefox(executable_path = binary, firefox_profile = profile)

Related

FireFox keeps asking, "What should Firefox do with this file?" What's the about:config setting in need to change to autodownload?

I'm trying to auto download documents with Firefox via Selenium and Python. I've made some attempts at stopping the automatically generated box but Firefox keeps asking, "What should Firefox do with this file?". What's the about:config setting I need to change or add to the Firefox profile so that I don't get this prompt box? My existing code is below:
""
autodlprofile = webdriver.FirefoxProfile()
autodlprofile.set_preference("browser.download.folderList", 2)
#autodlproflie.set_preference("browser.download.alwaysOpenInSystemViewerContextMenuItem", False)
autodlprofile.set_preference("browser.download.dir", r"C:\Users\X\Desktop\Automate the Boring Stuff\taxbills")
autodlprofile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
autodlprofile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream")
autodlprofile.set_preference("browser.helperApps.showOpenOptionForPdfJS", False)
""
Thank you for your time.
This setting is tested and works as expected (I tested it with firefox version 88.0.1, geckodriver 0.30.0 and Python 3.8.10.
Important Note: be careful if you will use very recent Firefox versions (> 90), I think there might be isssues with the automation of download features).
import SeleniumLibrary
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def create_profile(self)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.download.dir","**provide_download_dir_here**")
fp.set_preference("browser.helperApps.neverAsk.openFile",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.update_preferences()
return fp.path
# Add the following code where you open the browser
ff = self.create_profile()
BuiltIn().get_library_instance("SeleniumLibrary").open_browser("https://your_url","F irefox",ff_profile_dir=ff)

Python Firefox Selenium Load Profile

I tried many combinations of Geckodrive,Selenium and Python but i could not achieved to load Firefox profile what i need.It loads the default profile.
The lastest code is below and I opened an issue on github Selenium page but there is still no solution.There are some solutions about Java but I am not able to bind it in Python.A temp file created on temp folder which includes profile data and cookies.That mean selenium does not use any profile's cookies and other configurations
Any solutions will be apprecited.
Geckodrive version:0.23
Selenium 3.14
Python 3.7
Firefox 61
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.support.ui import WebDriverWait
fp = FirefoxProfile('C:\\Users\\<USER>\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ji8rbij6.test')
fp.update_preferences()
browser = webdriver.Firefox(firefox_profile=fp)
Thanks in advance
PS. I tried this code block in 3 different computer because of being able to have a problem on my own computer
This exact setup works as expected for me (windows10) and I'm able to load custom firefox profiles.
Firefox: 88.0.1
Geckodriver: 0.30.0 (geckodriver releases
Python: 3.8.10
robotframework-selenium2library: 3.0.0
robotframework-seleniumlibrary: 6.0.0
import SeleniumLibrary
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def create_profile(self)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.download.dir","**provide_download_dir_here**")
fp.set_preference("browser.helperApps.neverAsk.openFile",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.update_preferences()
return fp.path
# Add the following code where you open the browser
ff = self.create_profile()
BuiltIn().get_library_instance("SeleniumLibrary").open_browser("https://your_url","F irefox",ff_profile_dir=ff)

Automatically download for every file type on firefox using selenium python

I've noticed this general format for auto saving in selenium in other posts
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'PATH TO DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
But I'm downloading files from a source where I won't know what type will the files be. I've tried regex but it does not seem to work either
As to my knowledge of Selenium, there isn't a way to download all file types. You will have to list out all the required file types.
Here is a list of types: Link
Edit: Try the suggestions provided here: how can i download a file automatically on click of a link
Here I'm posting my suggestion for this issue, taking as an example the Brazilian Covid-19 files from the Ministry of Health:
from selenium import webdriver
from selenium.webdriver.common.by import By
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.manager.showWhenStarting", False)
firefox_options.set_preference("browser.download.dir", 'your directory') #Do not forget to add your download path!
firefox_options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-7z-compressed;application/x-rar-compressed;application/zip")
driver = webdriver.Firefox(options=firefox_options)
driver.get("https://covid.saude.gov.br")
csv_button=driver.find_element(By.XPATH,"/html/body/app-root/ion-app/ion-router-outlet/app-home/ion-content/div[1]/div[2]/ion-button")
csv_button.click()
I hope this code might help someone with the same issue.
For me it was the combination of falsing the alwaysAsk and never ask to save and then specifying the file extension.
set_preference("browser.helperApps.alwaysAsk.force"), False
set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip;application/gz")
se = Service(gecko_path)
op = webdriver.FirefoxOptions()
op.add_argument('--headless')
op.set_preference("browser.download.folderList", 2)
op.set_preference("browser.download.manager.showWhenStarting", False)
op.set_preference("browser.download.dir", my_dir)
op.set_preference("browser.helperApps.alwaysAsk.force", False)
op.set_preference("browser.download.improvements_to_download_panel", True)
op.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip;application/gz")
driver = webdriver.Firefox(service=se, options=op)
basically what Abdinardo Oliveira said.

how can i remove notifications and alerts from browser? selenium python 2.7.7

I am trying to submit information in a webpage, but selenium throws this error:
UnexpectedAlertPresentException: Alert Text: This page is asking you
to confirm that you want to leave - data you have entered may not be
saved. ,
>
It's not a leave notification; here is a pic of the notification -
.
If I click in never show this notification again, my action doesn't get saved; is there a way to save it or disable all notifications?
edit: I'm using firefox.
You can disable the browser notifications, using chrome options. Sample code below:
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
With the latest version of Firefox the above preferences didn't work.
Below is the solution which disable notifications using Firefox object
_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)
Disable notifications when using Remote Object:
webdriver.Remote(desired_capabilities=_desired_caps, command_executor=_url, options=_custom_options, browser_profile=_browser_profile)
selenium==3.11.0
Usually with browser settings like this, any changes you make are going to get throws away the next time Selenium starts up a new browser instance.
Are you using a dedicated Firefox profile to run your selenium tests? If so, in that Firefox profile, set this setting to what you want and then close the browser. That should properly save it for its next use. You will need to tell Selenium to use this profile though, thats done by SetCapabilities when you start the driver session.
This will do it:
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(firefox_options=options)
For Google Chrome and v3 of Selenium you may receive "DeprecationWarning: use options instead of chrome_options", so you will want to do the following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
Note: I am using webdriver-manager, but this also works with specifying the executable_path.
This answer is an improvement on TH Todorov code snippet, based on what is working as of Chrome (Version 80.0.3987.163).
lk = os.path.join(os.getcwd(), "chromedriver",) --> in this line you provide the link to the chromedriver, which you can download from chromedrive link
import os
from selenium import webdriver
lk = os.path.join(os.getcwd(), "chromedriver",)
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(lk, options=chrome_options)

Download file from sharepoint using selenium webdriver python

I am trying to download file from sharepoint url and written code to neverask.savetodisk but still it is showing dialog to save file. I tried same code and it works when we click download link from other URL but not working with sharepoint application. Here is code what i used...
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
# 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.defaultFolder",'tt_at');
profile.set_preference("browser.download.lastDir",'tt_at');
profile.set_preference('browser.download.dir', 'tt_at')
profile.set_preference("browser.download.useDownloadDir",True);
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/octet-stream,application/msexcel")
browser = webdriver.Firefox(profile)
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence")
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click()
but this above code still showing dialog to select location.
I think I got the solution, try the following:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = webdriver.FirefoxProfile()
#Give Complete Path of download dir
profile.set_preference("browser.download.lastDir",r'd:\temp')
profile.set_preference("browser.download.useDownloadDir",True)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream")
profile.set_preference('browser.helperApps.neverAsk.openFile', "application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream")
browser = webdriver.Firefox(profile)
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence")
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click()
If this is also not working for you, do the following, install the addon tamperdata in firefox and observe the content type for the file you are trying to download and then add that exact text to "browser.helperApps.neverAsk.*" preference. That shall solve your problem!

Categories