How to disable file download popup in firefox using selenium and python? - python

Im trying to download some reports from web page, using selenium and python
when i click on download link firefox shows save/open dialog
I have used firefox profile configuration as follows
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf,attachment/pdf")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,attachment/pdf")
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
self.browser = webdriver.Firefox(profile)
but still it shows message box,
i have changed
self.browser = webdriver.Firefox(firefox_profile=profile)
like above but no chance >
Can any one help me to resolve this?

I have a little helper function which works for me.
It uses chromedriver though.
def driver_download(location_for_download):
# options = Options()
# options.headless = True
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': location_for_download}
chrome_options.add_experimental_option('prefs', prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=chrome_options)
return driver
Note: For my particular application, I was unable to use the headless. But it should work if its a direct link to the file.

Related

Selenium: Driver ignores custom profile when headless mode is enabled

I have the following code:
options = webdriver.ChromeOptions()
options.add_argument('log-level=3')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--user-data-dir=C:\\users\\eirik\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--profile-directory=Bot")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path="C:\\Program Files\\chromedriver.exe", options=options)
This code functions properly, however, If I were to add options.add_argument("--headless") the browser will work as expected with the exception of the custom profile being completely ignored. So browser presets, such as logins and extensions are missing.
I'd be glad for any help!
Kind Regards
-Eirik

Setting options parameter for Selenium Firefox webdriver

I'm using Selenium's webdriver and GeckoDriverManager to open a website and take a screenshot in Firefox. That part works fine. I wanted to specify where to save this screenshot using selenium.webdriver.set_preference() to specify the download path and saving this under an options variable. If I use firefox_driver=webdriver.Firefox(service=service, options=options) the website no longer launches. This is the code I used to set the option preferences: https://stackoverflow.com/a/69974916
Does anyone have an idea where the problem might be coming from?
def load_driver():
service=Service(GeckoDriverManager().install())
options = Options()
options.set_preference("browser.helperApps.alwaysAsk.force", False)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.helperApps.neverAsk.openFile", "image/png")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/png")
options.set_preference("browser.download.folderList", 1)
firefox_driver = webdriver.Firefox(service=service) #stops working if I add options=options parameter here
def getscreenshot():
driver = load_driver()
driver.get(website)
sleep(3)
driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")

chrome in headless mode with selenium

How to open chrome in headless mode with selenium? I tried
chromeOptions = Options()
chromeOptions.add_argument("headless")
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)
but this just returns an error
Error with Permissions-Policy header: Unrecognized feature: 'interest-cohort'.", source: (0)
This is wrong way
chromeOptions.add_argument("headless")
try this
chromeOptions.add_argument("--headless")
I am using these config in my project, so thought it would be helpful to you.
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path = driver_path, options = options)
Note that driver_path is basically the chromedriver.exe, if ChromeDriverManager().install() works for you better stick with that.
headless is not a correct command format. Chrome command line switch always starts with -- that means it should be --headless
--headless: Run in headless mode, i.e., without a UI or display server dependencies.
Code:
chromeOptions = Options()
chromeOptions.add_argument("--headless")
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)
Please refer the below page for all command line switches and their details,
Reference: https://peter.sh/experiments/chromium-command-line-switches/
i'm using undetected_chrome, i used the below code to do mine and it worked perfectly
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
driver = uc.Chrome(use_subprocess=True, options=options)

How to use Firefox profiles in Selenium Python (windows)

I'm unable lo load my Firefox profile on the Selenium Webdriver.
i have tried several ways commented here on SO, but non did the trick. Either i loose connection to the driver or the profile does not get loaded.
this is what i have so far:
from selenium import webdriver
options = Options()
options.headless = False
fp = webdriver.FirefoxProfile(r'C:\\Users\\xxXX\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\y73h6ogb.default-release')
driver = webdriver.Firefox(fp, options=options, executable_path='geckodriver/geckodriver.exe')
driver.get(URL)
According to the documentation you can load a profile like this:
from selenium.webdriver import Firefox, FirefoxOptions, FirefoxProfile
options = FirefoxOptions()
options.headless = True
profile = FirefoxProfile("PATH_TO_PROFILE\\1234asdf.some_profile")
driver = Firefox(
firefox_profile=profile,
executable_path="PATH_TO_DRIVER\\geckodriver.exe",
options=options,
)
driver.get("https://python.org")
driver.close()
Or by adding the profile to the options:
...
options = FirefoxOptions()
options.headless = True
options.profile = FirefoxProfile("PATH_TO_PROFILE\\1234asdf.some_profile")
driver = Firefox(
executable_path="PATH_TO_DRIVER\\geckodriver.exe",
options=options,
)
...
By using webdriver for firefox, You will be able to do that!

Selenium in Python: Setting Firefox profile to webdriver does not seem to work

I want to be able to download a file from a website with a Python script by using Selenium. My problem is that creating and setting a Firefox Profile to the Firefox webdriver instance does not seem to work... Here is my code:
profile = webdriver.FirefoxProfile();
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/xml");
profile.update_preferences();
browser = webdriver.Firefox(firefox_profile=profile);
Even with the settings above, the open/save dialog box still opens up and the download does not happen. I then tried to do something simpler:
profile = webdriver.FireforProfile();
profile.set_preference("browser.startup.homepage", "http://www.google.com");
profile.update_preferences();
browser = webdriver.Firefox(firefox_profile=profile);
And all i get is firefox to open, that's it. This brings me to believe that the profile is not being set to the driver. Any help would be appreciated!
For anyone who comes along this here is a function that will return a webdriver with some common options. It shouldn't have a hardcoded path to the Firefox profile but I haven't gotten around to that yet
def get_firefox_driver(download_dir=None, use_profile=False, headless=False):
if download_dir is None:
download_dir = os.getcwd()
if use_profile:
try:
fp = webdriver.FirefoxProfile(r'C:\Users\MDDT0040\AppData\Roaming\Mozilla\Firefox\Profiles\u5jvcbqp.dev-edition-default-1525796820341',)
except:
print('cant get profile')
fp = webdriver.FirefoxProfile()
else:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", int(2))
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.manager.showAlertOnComplete", False)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference('browser.helperApps.neverAsk.saveToDisk',
"text/csv,text/comma-separated-values,text/comma-separated-values;charset=UTF-8")
if headless:
options = Options()
options.headless = True
return webdriver.Firefox(firefox_profile=fp, firefox_options=options)
else:
return webdriver.Firefox(firefox_profile=fp)

Categories