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)
Related
I use Selenium on Python and tried to use Firefox driver (as Chrome is too slow), but for some reasons Firefox driver always opens a new URL in a separate window, not a tab. Here is my driver initialization:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
options = webdriver.firefox.options.Options()
options.set_preference('intl.accept_languages', 'en-GB')
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_profile=profile, options=options)
And then it is just open each URL with window.open.
for url in page_lists:
driver.execute_script('window.open("{0}", "_blank");'.format(url))
This opens each URL in a new tab in Chrome driver, but how can I make Firefox driver do the same thing?
I tried adding these preferences according to this documentation, but none of them worked. In fact, the browser does not have a check-tick on the "open in a new tab" on the preferences page.
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
profile.set_preference("browser.link.open_external", 3)
profile.set_preference("browser.block.target_new_window", True)
On newer versions of selenium, you can use the following to open a new tab in the same window:
driver.switch_to.new_window("tab")
Then you would use the following to open a URL in that tab:
driver.get("URL")
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 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
With selenium I try to download something (in order to verify its content), using the following code as a proof-of-concept:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
#Set Location to store files after downloading.
profile.set_preference("browser.download.dir", "/tmp")
profile.set_preference("browser.download.folderList", 2)
#Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
#profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
# "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;")
profile.set_preference("browser.download.manager.showWhenStarting", False )
profile.set_preference("pdfjs.disabled", True )
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/zip")
profile.set_preference("plugin.disable_full_page_plugin_for_types", "application/zip")
browser = webdriver.Firefox(profile)
browser.implicitly_wait(10)
browser.get('https://www.thinkbroadband.com/download')
time.sleep(15)
elem = browser.find_element_by_xpath('//a[#href="http://ipv4.download.thinkbroadband.com/5MB.zip"]')
elem.click()
time.sleep(15)
However, nothing 'happens' (i.e. the download is not performed), and also no error message is shown. When I click on that download link manually, the test-file is being downloaded into /tmp.
Is there anything I am missing?
The issue could be because the click in this case needs to go through a child element
elem = browser.find_element_by_xpath('//a[#href="http://ipv4.download.thinkbroadband.com/5MB.zip"]/img')
elem.click()
But otherwise when you click on a link browser checks stuff in background for that link, and the site seems to have a problem, when I open the target link in browser I get an empty response
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