关于python:在chromedriver中禁用PDF Viewer插件

Disabling PDF Viewer plugin in chromedriver

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

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

我可以更改驱动程序设置来更改此行为吗?又如何?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

Chrome 57更改设置...
使用此功能禁用Chrome PDF查看器:

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


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

1
2
3
4
5
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)

下载现在可以正常工作了!


您可以将插件设置为禁用吗?

1
2
3
4
5
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)

请参阅"在Python中使用Selenium Webdriver设置Chrome偏好设置"和"如何使用Java禁用Selenium WebDriver中的Chrome插件"。