public static FirefoxProfile FirefoxDriverProfile() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir",Constant.downloadPath);
profile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv;application/vnd.openxmlformats-officedocument.wordprocessingml.document");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv;application/vnd.openxmlformats-officedocument.wordprocessingml.document");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete",false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
profile.setPreference("pdfjs.disabled", true);
return profile;
}
@Test
public static synchronized WebDriver getDriverInstance(String browser) throws Exception{
// If the browser is Firefox, then do this
if (Constant.BROWSER_FIREFOX.equalsIgnoreCase(browser)) {
fd = new FirefoxDriver(FirefoxDriverProfile());
fd.manage().window().maximize();
fd.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
}
}
以上是我的自动下载文件的代码,但没有显示弹出窗口,但它无法正常工作。 我已经从各种网站和stackoverflow上给出的答案中提取了参考资料。我是硒的新手。
答案 0 :(得分:0)
如果那是你的所有代码,我不明白你怎么期望它下载任何东西。尝试使用Robot()
而不是强制禁用窗口。这是我的代码示例:
public static void copyPaste (String content) throws AWTException {
//store the path (that you passing as 'content' String) to the saving folder in RAM
StringSelection selection = new StringSelection(content);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
//Initialize Robot()
Robot robot = new Robot();
//wait some time for save dialog to open
robot.delay(3000);
//imitate user's action - paste what you stored in RAM previously
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//submit this and download should start
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
打开下载窗口对话框后,可以使用此方法。