mozilla / geckodriver

WebDriver for Firefox
https://firefox-source-docs.mozilla.org/testing/geckodriver/
Mozilla Public License 2.0
7.11k stars 1.52k forks source link

Allow to prevent opening of files after download #1998

Open dschmidt opened 2 years ago

dschmidt commented 2 years ago

I currently cannot make selenium download a file without firefox opening it after the download. It works on my desktop system and I've tracked down the difference to be this setting in my handlers.json:

{
  "mimeTypes": {
    "application/x-rpm": {
            "action": 0,
            "extensions": [
                "rpm"
            ]
    }
}

(when I remove it from that file, it's opened automatically. If I readd it, the file is not opened anymore - so I'm pretty sure, this is the right/relevant setting)

I could not find a relevant preference that could be set through any non-deprecated api. Apparently @lukewiwa came to the same conclusion here: https://github.com/SeleniumHQ/selenium/issues/9997#issuecomment-967800646

I'd be super happy if this was possible already and I just couldn't figure it out... just let me know. Thanks in advance!

System

Testcase

const {Builder, By, Key, until} = require('selenium-webdriver');
const { Options  } = require('selenium-webdriver/firefox')

(async function helloSelenium() {
    const options = new Options()
        .setPreference('browser.download.folderList', 2) // custom location
        .setPreference('browser.download.manager.showWhenStarting', false)
        .setPreference('browser.download.dir', '/tmp/foobar')
        .setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/x-redhat-package-manager')
        .setPreference('browser.helperApps.neverAsk.openFile', 'application/x-redhat-package-manager')

    let driver = await new Builder().withCapabilities(options).build();
    driver.manage().setTimeouts({implicit: 3 })

    await driver.get('http://download.opensuse.org/tumbleweed/repo/src-oss/src/');

    const locator = By.css(".by-name > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(2) > a:nth-child(1)")
    let link = await driver.wait(until.elementLocated(locator),10000);
    await link.click()
})();

Stacktrace

Trace-level log

bswhb commented 1 year ago

Any news on this?

3ynm commented 1 year ago

this is needed, or an option to force downloads