如何使用selenium和java处理windows base文件下载弹出窗口和保存文件?
上面是我的代码自动下载文件而不显示windows弹出窗口,但它不工作。如何使用selenium和java处理windows base文件下载弹出窗口和保存文件?,java,selenium-webdriver,Java,Selenium Webdriver,上面是我的代码自动下载文件而不显示windows弹出窗口,但它不工作。 我从各种网站和网站上给出的答案中获取了参考资料。我是selenium的新手。如果你的代码都是这样,我不明白你希望它怎么下载任何东西。尝试使用Robot()而不是强制禁用windows。下面是我的代码示例: public static FirefoxProfile FirefoxDriverProfile() throws Exception { FirefoxProfile profile = new Firefox
我从各种网站和网站上给出的答案中获取了参考资料。我是selenium的新手。如果你的代码都是这样,我不明白你希望它怎么下载任何东西。尝试使用
Robot()
而不是强制禁用windows。下面是我的代码示例:
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);
}
}
您可以在打开下载窗口对话框后使用此方法。什么不起作用?修复了缩进
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);
}