How to hide the warning “This type of file can harm your computer” while downloading .xml file using Chrome Chromedriver 79 with Selenium Java

拈花ヽ惹草 提交于 2021-02-05 06:38:06

问题


Despite setting safebrowsing.enabled to true / false, the warning ...This type of file can harm your computer... is still being displayed in browser. How to hide this information?


回答1:


To enable downloading of file using Chrome/ChromeDriver hiding the warning This type of file can harm your computer you need to:

  • Add the preferences:
    • download.default_directory
    • download.prompt_for_download
    • download.extensions_to_open
    • safebrowsing.enabled
  • As well as add the following arguments to whilelist:
    • --safebrowsing-disable-download-protection
    • safebrowsing-disable-extension-blacklist

Demonstration

To demonstrate downloading using selenium-chromedriver, and google-chrome through java I have clicked on the first Download link in the webpage http://www.landxmlproject.org/file-cabinet and your effective solution will be:

  • Code Block:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.default_directory", "C:/Utility/Downloads/");
    prefs.put("download.prompt_for_download", false);
    prefs.put("download.extensions_to_open", "application/xml");
    prefs.put("safebrowsing.enabled", true);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    options.addArguments("start-maximized");
    options.addArguments("--safebrowsing-disable-download-protection");
    options.addArguments("safebrowsing-disable-extension-blacklist");
    WebDriver driver =  new ChromeDriver(options); 
    driver.get("http://www.landxmlproject.org/file-cabinet");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='MntnRoad.xml']//following::span[1]//a[text()='Download']"))).click();
    
  • Browser Snapshot:



来源:https://stackoverflow.com/questions/59840198/how-to-hide-the-warning-this-type-of-file-can-harm-your-computer-while-downloa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!