用于保存文件的Selenium firefox配置文件

时间:2012-10-06 11:11:35

标签: java firefox selenium automation

  

可能重复:
  Access to file download dialog in Firefox

我正在使用selenium和firefox从互联网上下载文件。 当我试图下载文件时,我收到下载框,询问我是否要保存文件或“打开”。 我想保存文件但不自动(想要重命名文件名),我希望浏览器会问我在哪里保存文件。 在firefox设置中选择“总是询问保存文件的位置”这个选项仍然是..当我使用selenium运行脚本时,它不会询问我并保存文件。 如何设置firefox配置文件来执行此操作?我在哪里可以看到所有firefox配置文件? 帮帮忙。

2 个答案:

答案 0 :(得分:4)

研究了问题的一小部分解决方法。想分享我发现的东西。 关于使用Selenium的自动化浏览器对话框: 没有简单的方法来制作Selenium下载文件,因为浏览器使用了无法通过JavaScript控制的原生对话框,所以你需要一些“黑客”。 查看this

特别是关于ffox浏览器设置,您可以将Firefox配置为特定位置的automatically start the download and save the file

或者使用它:

    FirefoxProfile firefoxProfile = new FirefoxProfile();

    firefoxProfile.setPreference("browser.download.folderList",2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
    firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

    WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);

    driver.navigate().to("http://www.myfile.com/hey.csv");

希望这适合你

答案 1 :(得分:0)

selenium使用的firefox配置文件与firefox附带的默认选项一起使用。您必须在selenium代码中设置要求,保存文件的位置选项。

相关问题