【问题标题】:Downloading a script file using selenium giving, This type of file can harm your computer popup使用 selenium give 下载脚本文件,这种类型的文件可能会损害您的计算机弹出窗口
【发布时间】:2019-07-31 11:09:36
【问题描述】:

使用 selenium webdriver 下载 python 脚本文件时,会弹出一个提示“这种类型的文件可能会损害您的计算机......”然后保留并丢弃按钮。我不想要那个弹出窗口。单击下载时,它应该在没有弹出窗口的情况下下载。我在 Chrome 75 版本中运行脚本。

并尝试放置

chromePrefs.put("safebrowsing.enabled", "false");
options.addArguments("--safebrowsing-disable-extension-blacklist");
options.addArguments("--safebrowsing-disable-download-protection");

这在初始化驱动程序时,但对我没有任何作用。

【问题讨论】:

    标签: java selenium-webdriver


    【解决方案1】:
    //Boilerplate code for setting driver and download path
    System.setProperty("webdriver.chrome.driver", "--driver path--");
    String path = "Download path";
    HashMap<String, Object> prefs = new HashMap<String, Object>();
    //setting browser preference values such as popup and download path 
    chromePrefs.put("profile.managed_default_content_settings.popups", 2);
    chromePrefs.put("safebrowsing.enabled", "true");
    chromePrefs.put("download.default_directory", path);
    
    //Boilerplate code for setting preferences in chrome options
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(capabilities);
    

    上面的代码 sn-p 应该可以帮助您创建一个 chrome 驱动程序,并禁用弹出窗口功能。

    为了让它在不同版本的 chrome 上工作,你可能不得不玩弄 “managed_default_content_settings.popups”或 “profile.default_content_settings.popups” 属性及其值(0,1 或 2)。

    希望对你有帮助!!

    【讨论】:

    • 之后用一堆代码“试试这个”并不是很有帮助。您已经添加了很多代码,但没有解释为什么要添加任何代码或它应该做什么。添加对您添加的行的一些描述以及为什么可能会使这成为一个很好的答案。
    • 感谢您的反馈@JeffC,将为其添加更多描述。
    • @Vishal Kumar 即使我尝试了所有组合,但对我没有任何效果
    猜你喜欢
    • 2011-09-10
    • 2019-08-19
    • 2016-10-04
    • 1970-01-01
    • 2016-03-11
    • 2018-07-19
    • 2020-10-30
    相关资源
    最近更新 更多