【问题标题】:Setting Selenium FirefoxProfile settings not consistent设置 Selenium FirefoxProfile 设置不一致
【发布时间】:2018-07-05 19:09:04
【问题描述】:

我正在尝试设置一个 Firefox(Windows, 61.0) 配置文件以在带有 Java 的 Selenium WebDriver (3.13.0) 中使用,以便 Firefox 自动下载文件,这样我就可以绕过下载对话框。

代码如下:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.useDownloadDir",true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/x-download");
return options.setProfile(profile);

browser.download.folderListbrowser.helperApps.neverAsk.saveToDisk 都会影响您希望它们影响的设置,但在 browser.download.useDownloadDir 的情况下,它不会影响实际设置(即它仍然为 false)。相反,它会创建一个新的类似 (?) 设置,称为 services.sync.prefs.sync.browser.download.useDownloadDir

知道这里的问题是什么,以及如何将 useDownloadDir 设置设置为true

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-firefoxdriver


    【解决方案1】:

    选项 1: 指定下载文件的 MIME 类型。这是 XLS / XLSX 文件的示例:

    FirefoxProfile selenium_profile = new FirefoxProfile();
    selenium_profile.setPreference("browser.download.folderList",2);
    selenium_profile.setPreference("browser.download.dir", "C:\\Users\\pburgr\\Desktop\\BP_usr_tmp\\");
    selenium_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    options.setProfile(selenium_profile);
    

    选项 2:使用现有的 Firefox 配置文件。使用现有配置文件“selenium_profile”我使用这个:

    @BeforeClass
        public static void setUpClass() {
            FirefoxOptions options = new FirefoxOptions();
            ProfilesIni allProfiles = new ProfilesIni();         
            FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
            options.setProfile(selenium_profile);
            options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
            driver = new FirefoxDriver(options);
            driver.manage().window().maximize();
            }
    

    使用 Firefox 配置文件管理器 (Win+R: firefox -p) 创建一个新配置文件。在新配置文件中运行 Firefox 并设置您需要的自定义项,包括特定文件类型的自动下载。

    【讨论】:

      【解决方案2】:

      我可以通过在运行时设置这些首选项来解决这个问题,如本文所述:

      Selenium firefox profile update download directory after creating webdriver

      我的问题可能是由我机器上的企业设置引起的,无论通过 Selenium 传递给浏览器的内容如何,​​每次打开新窗口时都可能强制首选项以某种方式默认。例如,我无法编辑 browser.download.useDownloadDir、browser.download.dir 和 browser.download.folderList(以及其他)。我的 mime 类型设置正确,但问题仍然存在。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        相关资源
        最近更新 更多