0

In the below code we are trying to capture the information when the chrome opens the file, but it is failing in some cases as some log files are getting downloaded and it's not viewable in chrome.

Let me know if any preference has to be added so that all files (irrespective of size) can be either viewed or downloaded.

public class DownloadFile {                                                                              
    String FILE_ADD = "file:///";                                                                        
    String LOG_PATH = "path";                                                            
    String LOG_FILE = "sample.log";                                  
    String OUTPUT_FILE = "output.txt";                                                          
                                                                                                         
    public static RemoteWebDriver driver;                                                                
    @BeforeEach                                                                                          
    public void setUp() throws Exception {                                                               
        System.setProperty("webdriver.chrome.driver","chromedriver.exe");
        ChromeOptions options = new ChromeOptions();                                                     
                                                                                                         
        //Map<String, Object> prefs = new HashMap<String, Object>();                                     
        //prefs.put("download.prompt_for_download", false);                                              
        //prefs.put("log.disabled", true);                                                               
        //options.setExperimentalOption("prefs", prefs);                                                 
                                                                                                         
        //driver = new ChromeDriver(options);                                                            
        //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);                               
        //driver.manage().window().maximize();                                                           
        String fileDownloadPath = "downnloadpath";                             
                                                                                                         
        Map<String, Object> prefsMap = new HashMap<String, Object>();                                    
        prefsMap.put("profile.default_content_settings.popups", 0);                                      
        prefsMap.put("download.default_directory", fileDownloadPath);                                    
        prefsMap.put("txtjs.disabled", true);                                                            
                                                                                                         
        ChromeOptions option = new ChromeOptions();                                                      
        option.setExperimentalOption("prefs", prefsMap);                                                 
        option.addArguments("--test-type");                                                              
        option.addArguments("--disable-extensions");                                                     
        //Any preference to be added to download all the files instead of viewing some files in chrome                                                        
        driver  = new ChromeDriver(option);                                                              
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);                                 
        driver.manage().window().maximize();                                                             
    }                                                                                                    
                                                                                                         
    @Test                                                                                                
    public void fileDownload() throws AWTException, InterruptedException, IOException {                  
        driver.get(FILE_ADD+LOG_PATH+LOG_FILE);                                                          
        //String output = driver.findElement(By.xpath("/html/body/pre")).getText();                      
        //String final_output = output.replaceAll("User.*>", "testing");                                
        //FileUtils.writeFile(LOG_PATH+OUTPUT_FILE, output);                                             
        Thread.sleep(7000);                                                                              
                                                                                                         
    }                                                                                                    
    @AfterEach                                                                                           
    public void tearDown() throws Exception {                                                            
        driver.quit();                                                                                   
    }                                                                                                    
}   
Radha
  • 3
  • 4

1 Answers1

0

To my knowledge, you can not have this custom, where you can set it to view for 1 file, or download for the next, unless you override the ChromeDriver options below you download...

The below is if you wish to download the file/without a dialog box:

download.prompt_for_download is what you should use.

String downloadFilepath = "C:\\DownloadDirectoryPath"

HashMap<String, Object> chromePreferences = new HashMap<String, Object>();

chromePreferences.put("profile.default_content_settings.popups", 0);
chromePreferences.put("download.prompt_for_download", "false");
chromePreferences.put("download.default_directory", downloadFilepath);

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", chromePreferences);

=== Edited ===

You could try to use the option:

chromePreferences.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );

Are you using setExperimentalOption? What is your ChromeDriver version?

Using: ChromeDriver 91, you can look into your Preferences file under the directory: C:\Users<yourName>\AppData\Local\Google\Chrome\User Data\Default

The below URLs, Chrome does not popup a Download Dialog box.

In my Preferences file, I have the following as an example:

            "automatic_downloads": {
                "https://folkets-lexikon.csc.kth.se:443,*": {
                    "last_modified": "13182517278621031",
                    "setting": 1
                },
                "https://mail.google.com:443,*": {
                    "last_modified": "13160578764124505",
                    "setting": 1
                }
            },

As a reference, the below are helpful:

Disable chrome download multiple files confirmation

Could not set download.prompt_for_download false for avoiding popup when downloading a file in an Electron application

How to use chrome webdriver in selenium to download files in python?

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • https://stackoverflow.com/questions/61852540/could-not-set-download-prompt-for-download-false-for-avoiding-popup-when-downloa for more options – JCompetence Jun 16 '21 at 08:42
  • I have 2 files 1) file:///path/file.log -- It is getting downloaded 2) file:///path/file1.log -- It is getting viewed I tried prompt_for_download preference, I could download file.log but when I run for file1.log I am getting below error in chrome Your file couldn’t be accessedIt may have been moved, edited, or deleted. ERR_FILE_NOT_FOUND – Radha Jun 16 '21 at 08:48
  • That preference will just give the popup for saving the file, but In my case, it is not asking for anything just viewing the file in chrome – Radha Jun 16 '21 at 08:57
  • Edited, see if it helps @Radha – JCompetence Jun 16 '21 at 09:24
  • I did add "profile.content_settings" but don't see any difference and we have setExperimentalOption as well. – Radha Jun 16 '21 at 09:50
  • Interesting. You could check if you have this file already downloaded before? And perhaps that is why Chrome is viewing it? Clearing cache? Try a different file, is it only this file:///path/file1.log that is causing problems or others? – JCompetence Jun 16 '21 at 09:53
  • Unfortunately, it is the behavior of some files in our environment some are getting downloaded others are viewable in chrome, with the commented code, we are viewing the file and grabbing the text displayed in the chrome and for some other files it is downloaded above code won't work, we need something reliable for both types of files – Radha Jun 16 '21 at 09:57