我试图建立在硒的webdriver(3.13.0)与Java使用火狐(Windows中,61.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.folderList
和browser.helperApps.neverAsk.saveToDisk
都会影响你希望他们影响的设置,但在browser.download.useDownloadDir
的情况下,它不会影响实际的设置(即它仍然是假的)。相反,它创建了一个新的,类似的(?)设定称为services.sync.prefs.sync.browser.download.useDownloadDir
。
任何想法是什么问题就在这里,和我如何可以设置useDownloadDir设置为true
?
选项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:火狐-p)创建一个新的配置文件。在新的配置文件运行Firefox和设置您需要包括特定的文件类型autodownload自定义。
我能够在这个职位描述的设置在运行时的首选项来解决这个问题:
Selenium firefox profile update download directory after creating webdriver
我的问题很可能是由于我的机器上的企业设置,这可能会迫使首选项每个新窗口中打开,无论什么传递给通过Selenium的浏览器时默认以某种方式引起的。例如,我是不是能够编辑browser.download.useDownloadDir,browser.download.dir和browser.download.folderList(等等)。我的MIME类型进行正确设置和问题仍然出现在我结束。