【问题标题】:How to edit the anonymous/default profile in firefox?如何在 Firefox 中编辑匿名/默认配置文件?
【发布时间】:2015-05-05 02:30:02
【问题描述】:

我正在使用 Selenium 来自动化下载文件的测试用例。 在 Firefox 中,当我单击链接下载文件时,会出现一个对话框来保存或打开文件。我去了 Firefox 并在地址栏中输入 about:config 并使用 mimetype 更新了 browser.helperapps.neverask.savetodisk。当我手动单击链接下载文件时,它会自动下载而没有对话框。当我使用 Selenium 自动化它时,会出现对话框。任何人都可以建议一种在 Firefox 中自动执行此操作而无需在代码中创建配置文件的方法。我不介意更改浏览器设置。在 Firefox 中如何更新 Selenium 使用的配置文件?

【问题讨论】:

  • 如果在代码中创建配置文件是选项...让我知道该怎么做我的代码如下@BeforeStory public void beforeStory() throws Exception { System.out.println("Initializing网络驱动程序"); driverProvider.initialize(); webDriver = driverProvider.get(); webDriver.manage().deleteAllCookies(); //指定驱动程序在搜索元素时应该等待的时间量,如果它不是立即存在的话。 webDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

标签: java firefox selenium


【解决方案1】:

在 Selenium WebDriver 中启动 firefox 时,它会启动新的匿名配置文件,您可以通过创建新配置文件并更新首选项或使用现有配置文件来修改它

FirefoxProfile profile = new FirefoxProfile(); 

profile.setPreference("browser.download.folderList", 2);

profile.setPreference("browser.download.manager.showWhenStarting", false);

profile.setPreference("browser.download.dir", **enter your download path**);

profile.setPreference("browser.helperApps.neverAsk.openFile",
            "text/csv, application/pdf, application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/excel");

profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
            "text/csv, application/pdf, application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel, application/vnd.ms- excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/excel,text/x-c");

Webdriver driver = new FirefoxDriver(profile);

如果您不希望在代码中创建 firefox 配置文件,请手动创建具有上述设置的 firefox 配置文件,并使用以下代码使用该配置文件

ProfilesIni all_profiles = new ProfilesIni();
FirefoxProfile profile = all_profiles.getProfile("created profile");
WebDriver driver = new FirefoxDriver(profile);

希望对你有所帮助....

【讨论】:

  • @BeforeStory public void beforeStory() throws Exception { System.out.println("Initializing WebDriver"); driverProvider.initialize(); webDriver = driverProvider.get(); webDriver.manage().deleteAllCookies(); //指定驱动程序在搜索元素时应该等待的时间量,如果它不是立即存在的话。 webDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
  • 您必须在创建 webdriver 实例之前放置代码...在上面的代码中,我创建或使用了一个 firefox 配置文件并将其传递给 FirefoxDriver 类...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2013-07-16
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多