I'm trying to automate a file download in a web page using selenium. We can consider for example that I will try to automate the download of geckodriver from https://github.com/mozilla/geckodriver/releases.
My python code is the following:
That's the code that I used:
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)
profile.set_preference("browser.download.downloadDir", path)
profile.set_preference("browser.download.defaultFolder", path)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip, application/tar+gzip, application/x-gtar, application/x-compressed")
profile.set_preference("pdfjs.disabled", True)
profile.update_preferences()
browser = webdriver.Firefox(profile)
browser.get('https://github.com/mozilla/geckodriver/releases')
sleep(5)
try:
field = browser.find_element_by_xpath("//ul[@class='release-downloads']//li/a/strong[contains(text(),'geckodriver-v0.11.1-linux64.tar.gz')]")
if(field != None):
field.click()
find = True
except Exception,e:
print str(e)
My problem is that if I try this code I got the popup download of firefox:
I thought that maybe I set bad value of the preference browser.helperApps.neverAsk.saveToDisk, so I tried to download the zip file but I got the same problem. Then I took look on about:config but unfortunately I found that parameters are set to default values. For example I found the value of browser.download.folderList is 1 but not 2:
also browser.helperApps.neverAsk.saveToDisk is null value, and browser.download.dir doesn't exist.
Edit: Download without setting preferences
Then I try without setting preferences with this code:
browser = webdriver.Firefox()
browser.get('https://github.com/mozilla/geckodriver/releases')
sleep(5)
try:
field = browser.find_element_by_xpath("//ul[@class='release-downloads']//li/a/strong[contains(text(),'geckodriver-v0.11.1-linux64.tar.gz')]")
if(field != None):
field.click()
find = True
except Exception,e:
print str(e)
pass
But I get the same problem like that setting preferences part that I deleted hasn't any influence. But taking in consideration that if I open Firefox manually and click manually in the download link I can download the file directly with the confirm popup of firefox without any problem.
Is there mistakes in my code? or what's the problem?
Development environment: Python2.7, selenium3.0.1, Firefox 49.