Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

How to handle file download popup using Selenium WebDriver?

I have written a code to download an excel file, it is working, but it stops as soon as the popup appears. Automatically it should download the file and store in specified location, which is not happening now. Please anyone help in finding a solution for this problem

FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile); 


profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "text/csv"); 
profile.setPreference("browser.download.folderList", 2); 
profile.setPreference("browser.download.dir","e:\SampleExcel"); 

driver.get("http://url");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("pass")).sendKeys("abc");
driver.findElement(By.id("edit-submit")).click();
driver.findElement(By.id("toolbar-link-admin-config")).click();
driver.findElement(By.linkText("Reports")).click();
driver.findElement(By.xpath("//input[@value='5']")).click();
driver.findElement(By.id("edit-submit")).click();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try below code

FirefoxProfile profile = new FirefoxProfile();
        String path = "D:\Downloads_sel";
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", path);
        profile.setPreference("browser.download.alertOnEXEOpen", false);
        profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        profile.setPreference("pdfjs.disabled", true);

        WebDriver driver = new FirefoxDriver(profile);

For complete MIME types list follow the link: http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...