【问题标题】:Disabling PDF Viewer plugin in chromedriver在 chromedriver 中禁用 PDF 查看器插件
【发布时间】:2017-06-12 03:24:58
【问题描述】:

我正在尝试在 BlackBoard 环境中批量下载大量文件(在世界各地的大学/学校中经常使用)。我能够检索文件所在的链接,但只有一个市长问题:

当文件是 .pdf 文件时,它会显示在新的浏览器选项卡中,而不是被下载。例如使用 click() 下载 .xlsx 文件效果很好..

我可以更改驱动程序设置以更改此行为吗?怎么做?

编辑
我根据 Ari 的回答更新了问题。它现在包含有关实际插件的更多信息。也许这可用于识别必须禁用的插件..

Chrome PDF Viewer (2 files)

   Name:            Chrome PDF Viewer
       Version: 
       Location:    chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/
       Type:        BROWSER PLUGIN
                    Disable
       MIME types:  MIME type        Description     File extensions
                    application/pdf                  .pdf

   Name:            Chrome PDF Viewer
       Description: Portable Document Format
       Version: 
       Location:    internal-pdf-viewer
       Type:        PPAPI (out-of-process)
                    Disable
       MIME types:  MIME type        Description    File extensions
                    application/x-google-chrome-pdf Portable Document Format    
                    .pdf   

【问题讨论】:

    标签: python python-2.7 selenium selenium-chromedriver


    【解决方案1】:

    Chrome 57 更改设置... 使用它来禁用 Chrome PDF 查看器:

    //To disable PDF viewer plugins with Chrome 57
    chromePrefs.put("plugins.always_open_pdf_externally", true);
    

    【讨论】:

    • 确实,'plugins.plugins_disabled': ["Chrome PDF Viewer"], 不再起作用(自 Chrome 57 起)!谢谢,您有正确的解决方案!你是怎么找到的?
    • 在 Python 中,使用:chrome_options = webdriver.ChromeOptions(); chrome_options.add_experimental_option("prefs", {"download.prompt_for_download": False, "plugins.always_open_pdf_externally": True})
    • 在Robotframework中,使用:${prefs} Create Dictionary download.default_directory ${CURDIR} plugins.always_open_pdf_externally ${TRUE}注意Create Dictionary${TRUE}的用法
    【解决方案2】:

    Ari 的回答几乎可以正常工作。我只需要将插件的名称封装到一个列表中:

    chromeOptions = webdriver.ChromeOptions()
    prefs = {"plugins.plugins_disabled" : ["Chrome PDF Viewer"]} # Here should be a list
    chromeOptions.add_experimental_option("prefs",prefs)
    chromedriver = "path/to/chromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    

    现在可以正常下载了!

    【讨论】:

    【解决方案3】:

    您可以将插件设置为禁用作为首选项吗?

    chromeOptions = webdriver.ChromeOptions()
    prefs = {"plugins.plugins_disabled" : "Chrome PDF Viewer"}
    chromeOptions.add_experimental_option("prefs",prefs)
    chromedriver = "path/to/chromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    

    参见“setting Chrome preferences w/ Selenium Webdriver in Python”和“How to disable Chrome Plugins in Selenium WebDriver using Java”。

    【讨论】:

    • 谢谢,但对我不起作用..当我检查它时它不会禁用插件。我更新了问题,现在包含有关实际插件的更多信息。也许这可用于识别必须禁用的插件..
    猜你喜欢
    • 2016-05-19
    • 2012-12-31
    • 1970-01-01
    • 2013-03-14
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多