我的代码在这里: -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.sample-videos.com/");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='sample-mp4-video']/table/tbody/tr[0]/td[4]")).click();
目标是处理Windows弹出窗口,以便将下载文件保存到本地驱动器。
有没有办法自动使用selenium
答案 0 :(得分:1)
您可以通过自定义首选项自动下载文件。您需要在browser.download.dir
中定义下载文件夹以及将下载的文件的MIME类型(在您的示例中为video/mp4
)。请注意,MIME类型是请求响应中返回的Content-Type
标头。
以下是您帖子中的页面的工作示例:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "C:\\Temp"); // folder
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "video/mp4"); // MIME type
profile.setPreference("pdfjs.disabled", true); // disable the built-in viewer
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.panel.shown", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, 1);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.sample-videos.com/");
// click on the link "10 mp4 720x480"
driver.findElement(By.xpath("//tr[td='10'][td='mp4'][td='720x480']//a")).click();
答案 1 :(得分:0)
您需要使用Firefox分析来克服此问题: -
FirefoxProfile pro=new FirefoxProfile();
pro.setPreference("browser.downLoad.folderList", 0);
pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");
WebDriver driver=new FirefoxDriver(pro);
browser.download.folderList控制要下载文件的默认文件夹。 0表示桌面; 1表示系统默认下载位置; 2表示自定义文件夹。 browser.download.dir包含自定义目标文件夹 下载。如果browser.download.folderList已设置为2,则会激活它。 browser.helperApps.neverAsk.saveToDisk存储以逗号分隔的MIME类型列表,以保存到磁盘,而不询问用于打开文件的内容。 下面是chrome多重下载选项的代码: -
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", getClass().getResource("/data/input").toString().replace("%20", " ").replace("file:","").replaceFirst("/", ""));
options.setExperimentalOption("prefs", prefs);
options.addArguments("--test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
对于IE,请参阅: -
http://9to5it.com/internet-explorer-disable-do-you-want-to-open-or-save-this-file-prompt/ 您也可以使用Robot类的java,但它可以锁定您的屏幕
您需要使用ROBOT类来触发ENTER Action事件。在java中,如果要激活任何事件,则必须使用Robot类以编程方式键入或触发ENTER和ESCAPE等事件。
// Create object of Robot class
Robot object=new Robot();
// Press Enter
object.keyPress(KeyEvent.VK_ENTER);
// Release Enter
object.keyRelease(KeyEvent.VK_ENTER);
希望它会对你有所帮助:)。
如果仍然面临问题,请回复我:)
答案 2 :(得分:0)
你可以使用firefox个人资料。
`public class FileDownloadExample {
public static String downloadPath = "D:\\seleniumdownloads";
@Test
public void testDownload() throws Exception {
WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());
driver.manage().window().maximize();
driver.get("http://URL");
driver.findElement(By.linkText("test.xls")).click();
}
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", downloadPath);
profile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
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);
return profile;
}}`
答案 3 :(得分:0)
使用此代码并尝试。它将自动下载。
WebDriver driver = new FirefoxDriver();
driver.get("http://www.sample-videos.com/");
driver.manage().window().maximize();
Thread.sleep(5000L);
driver.findElement(By.xpath(".//*[@id='sample-mp4-video']/table/tbody/tr[1]/td[4]/a")).click();
Thread.sleep(5000L);
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
回复我以进一步查询。 快乐学习。享受: - )
答案 4 :(得分:0)
我通过使用下面的代码片段
解决了这个问题 System.setProperty(GEKO_DRIVER, FIREFOX_DRIVER_LOCATION);
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
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);
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);