I am trying to download a PDF files using Firefox in Selenium but the preferences I have set below do not seem to be working. Whenever I run the code, I am still getting the "You have chosen to open:" dialog box even though the preferences state that PDF files should automatically be downloaded.
Am I missing something?
def setUp(self):
downloads_folder = initialSearch.download_path(self)
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", downloads_folder)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
self.driver = webdriver.Firefox(profile)
Try this one profile.update_preferences() before
webdriver.Firefox(profile)
please may help you
Related
I want to change web driver preferences so I can bypass the save/open prompt that pops up when the Firefox webdriver clicks on the download button for a pdf I want.
I am setting the preferences for the Firefox web driver and passing it as a parameter "options" when I initialize the webdriver. It shows that the preferences I enter save into options.preferences but when I have selenium click on the download button on the website, the download prompt for the pdf still pops up.
def __init__(self):
options = webdriver.FirefoxOptions()
options.headless = False
print(options.preferences)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", 'myDir')
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
print(options.preferences)
self.driver = webdriver.Firefox(options = options)
I plan to have this headless, but have it set to False for now. What would be causing the webdriver not to take in the preferences I passed into Options?
Fixed the issue. It seems that the firefox pdf previewer was bypassing the settings I was passing through. The line below blocked the preview of the pdf and allowed the automatic download to take effect.
options.set_preference("pdfjs.disabled", True)
I'm wondering how can I update/change download location in selenium once I started driver?
it is not problem to set download dir during creation of profile and initiation of webdriver. The problem appears after initiation of webdriver to change directory depending on data type.
For example
-if dl doc is word save in Docs\Word
-if dl doc is pdf save in Docs\pdf
this is my code
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference("browser.download.folderList", 2)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/download,application/octet-stream,application/pdf')
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.delete_all_cookies()
sleep(10)
# this part doesn't work
driver.profile.set_preference('browser.download.dir',"{0}\{1}".format(os.getcwd(),"Docs"))
driver.profile.update_preferences()
With Firefox it's possible to change the preferences at run-time with a scrip injection once the context is set to chrome:
def set_download_dir(driver, directory):
driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
Services.prefs.setStringPref('browser.download.dir', arguments[0]);
""", directory)
driver.execute("SET_CONTEXT", {"context": "content"})
I currently have a script that will log on to my company's wiki, visit a page, and select a download to pdf option available on the page. However, when this option is chosen, this dialogue box
pops up asking me to tell Firefox what to do with it. I just need selenium to interact and hit the "ok" button.
I'm not sure how to inspect this window for elements, and am need of direction. Any documentation helps.
from splinter import Browser
browser = Browser()
browser.visit('https://company.wiki.com')
browser.find_by_id('login-link').click()
browser.fill('os_username', 'user')
browser.fill('os_password', 'pass')
browser.find_by_name('login').click()
browser.visit('https://pageoncompany.wiki.com')
browser.find_by_xpath('//*[#id="navigation"]/ul/li[4]').click()
browser.find_by_id('action-export-pdf-link').click()
I was able to set the preferences through the web browser, then call my profile:
browser = Browser('firefox', profile=r'C:\Users\craab\AppData\Roaming\Mozilla\Firefox\Profiles\0lot9hun.default')
You can set preferences in order to prevent coming of download popup ad download it to pre-defined folder.
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2) # custom folder as set by repo
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", <download_folder_path>)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", content_type)
# Enable auto download, Avoid popup during downloads
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.helperApps.neverAsk.openFile", content_type)
driver = webdriver.Firefox(fp)
Trying to extract an image, successfully triggered "Save Image as..." dialog, but couldn't send any keys, is there a way to solve this problem?
driver = webdriver.Firefox()
actions = webdriver.ActionChains(driver)
actions.move_to_element(img).context_click(img).send_keys('v').perform()
time.sleep(2)
# and this line does not work
actions.send_keys('image.jpg').perform()
Only one step away from making everything works, what should I do?
This is a kind of popup you cannot control with selenium.
In this case you need to ask the browser to save the file automatically by tweaking it's preferences (aka desired capabilities):
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/file")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg")
driver = webdriver.Firefox(firefox_profile=profile)
where browser.helperApps.neverAsk.saveToDisk setting value should have a mime-type (or a comma-separated list of mime-types) of the files that should be downloaded automatically.
See also:
Access to file download dialog in Firefox
What is the format of the .tar.gz file for setting up firefox preference from selenium python?
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',('application/tgz'))
browser = webdriver.Firefox(firefox_profile = profile)
above code did not work.
Would be fine if someone has tried out it before.
After a bit googling, I got this you can give a try.
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/x-gzip')
Note that ".tar" has MIME type of application/x-tar, "tar.gz,tgz,gz" have application/x-gzip.
Also you might also want to add more about:config settings which may affect.
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/x-gzip')
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.dir", your_download_dir)
profile.set_preference("browser.download.manager.showWhenStarting", False)