访问Firefox中的文件下载对话框

And*_*kin 81 firefox selenium dialog

是否有任何类型的API可以让我在Firefox中操作文件下载对话框?(我想访问用户做某事时出现的那个,而不是自己发起一个).

我想要做的是从Selenium访问这个对话框(以及Selenium"特权模式"是否足以访问chrome接口是我不确定的事情).

dlo*_*lez 71

我有一个解决这个问题的方法,检查代码:

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");
Run Code Online (Sandbox Code Playgroud)

  • 并不是说这不适用于`Content-Disposition:attachment`.在这种情况下,Firefox似乎总是弹出一个对话框! (3认同)
  • 可能是值名称已更改,这是一年多以来. (2认同)

Ake*_*ndo 40

我遇到了同样的问题,但我找到了解决方案.我这样做的方式和博客一样.

当然这是Java,我把它翻译成Python:

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")

browser = webdriver.Firefox(firefox_profile=fp)
Run Code Online (Sandbox Code Playgroud)

在我的例子中,它是一个CSV文件.但是当你需要更多时,就会存储在~/.mozilla/$USER_PROFILE/mimeTypes.rdf

  • 我试过这个,但它似乎对我不起作用.文件保存对话框仍然显示. (2认同)

Aar*_*lla 32

从来没听说过.但您可以将Firefox配置为自动启动下载并将文件保存在特定位置.然后,您的测试可以检查文件是否实际到达.


Pra*_*ams 7

Web应用程序生成3种不同类型的弹出窗口; 即,

 1| JavaScript PopUps
 2| Browser PopUps
 3| Native OS PopUps [e.g., Windows Popup like Upload/Download]
Run Code Online (Sandbox Code Playgroud)

通常,JavaScript弹出窗口是由Web应用程序代码生成的.Selenium提供了一个API来处理这些JavaScript弹出窗口,例如Alert.

最终,忽略浏览器弹出和下载文件的最简单方法是使用浏览器配置文件完成; 有几种方法可以做到这一点:

  • 手动涉及浏览器属性的更改(或)
  • 使用配置文件setPreference自定义浏览器属性

方法1

在开始使用浏览器配置文件中的弹出窗口之前,请确保将"下载"选项默认设置为"保存文件".

(打开Firefox)工具>选项>应用程序

在此输入图像描述

方法2

使用以下代码段并在必要时进行编辑.

FirefoxProfile profile = new FirefoxProfile();

String path = "C:\\Test\\";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);  
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);

driver = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)


San*_*ani 5

我面临同样的问题.在我们的应用程序中,FireFox的实例是通过传递DesiredCapabilities创建的,如下所示

driver = new FirefoxDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)

基于其他人的建议,我做了我的改动

FirefoxProfile firefoxProfile = new FirefoxProfile();     
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
    "application/octet-stream");
driver = new FirefoxDrvier(firefoxProfile);
Run Code Online (Sandbox Code Playgroud)

这有助于达到目的但不幸的是我的其他自动化测试开始失败.原因是,我已经删除了之前通过的功能.

还有一些在网上浏览并找到了另一种方式.我们可以将配置文件本身设置为所需的功能.

所以新的工作代码看起来像

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// add more capabilities as per your need.
FirefoxProfile firefoxProfile = new FirefoxProfile();        
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
    "application/octet-stream");

// set the firefoxprofile as a capability
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
driver = new FirefoxDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)