微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Selenium Remote WebDriver 在 chrome 中下载日志文件而不是查看如果内存小于 200B,则某些文件在 chrome 中打开

如何解决如何使用 Selenium Remote WebDriver 在 chrome 中下载日志文件而不是查看如果内存小于 200B,则某些文件在 chrome 中打开

在下面的代码中,我们试图在 chrome 打开文件时捕获信息,但在某些情况下它会失败,因为某些日志文件正在下载并且在 chrome 中无法查看。

如果必须添加任何首选项,以便可以查看或下载所有文件(无论大小),请告诉我。

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();                                                                                   
    }                                                                                                    
}   

解决方法

据我所知,您不能拥有此自定义,您可以将其设置为查看 1 个文件或下载下一个文件,除非您覆盖下载下方的 ChromeDriver 选项...

下面是如果你想下载文件/没有对话框:

download.prompt_for_download 是您应该使用的。

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);

=== 编辑 ===

您可以尝试使用该选项:

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

你在使用 setExperimentalOption 吗?您的 ChromeDriver 版本是什么?

使用:ChromeDriver 91,您可以查看目录下的首选项文件:C:\Users\AppData\Local\Google\Chrome\User Data\Default

以下网址,Chrome 不会弹出下载对话框。

在我的首选项文件中,我以以下内容为例:

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

作为参考,以下内容很有帮助:

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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。