如何在 Chrome 中使用 Selenium + Python 绑定控制文件的下载 [英] How to control the download of files with Selenium + Python bindings in Chrome

查看:44
本文介绍了如何在 Chrome 中使用 Selenium + Python 绑定控制文件的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到描述可用于 Selenium 和 Chrome 网络浏览器的选项的文档?我想在网络浏览器中打开一个链接(以获取凭据),但不想下载相应的文件(.pdf 或 .tiff 或 .jpeg).我在 Windows 7 笔记本电脑上使用 Python 2.7、selenium 3.0.1 和 Chrome 版本 54.0.2840.99(和 chromedriver.exe).

Where can I find the documentation that describes the options I can use with Selenium and Chrome web browser? I want to open a link in a web browser (to get credential) but not to download the corresponding file (.pdf or .tiff or .jpeg). I am using Python 2.7, selenium 3.0.1 and Chrome version 54.0.2840.99 (and chromedriver.exe) on Windows 7 Laptop.

# Chrome web browser.
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')  
#options.add_argument('--disable-download-notification') #doesn't seems to work 
#options.add_experimental_option("prefs", {"download.default_directory","C:UsersxxxdownloadsTest"}) # doesn't work
#options.add_experimental_option("prefs", {"download.prompt_for_download": False}) # doesn't seems to work
#options.add_experimental_option("prefs", {'profile.default_content_settings': {'images': 2}})# this will disable image loading in the browser
options.add_argument("user-agent="+user_agent_profile)
driver_main = webdriver.Chrome(chrome_options=options)

# Opening the web application portail.
driver_main.get("https://my_link")

我发现了很多关于这个主题的讨论,但没有一个解决方案有效.例如:

I found many discussions on this topic but none of the solution works. For example:

add_experimental_option("prefs", {"download.default_directory","C:UsersxxxdownloadsTest"})

对我不起作用.

同样适用于:

add_experimental_option("prefs", {"download.prompt_for_download": False})

(我也尝试使用false").

(I also try with "false").

虽然:

add_argument("user-agent="+user_agent_profile)

似乎有效!

我不确定有什么问题

我遇到的问题是,每次我打开名称为 file(1) file(2) .... file(99) 的链接时,它都会开始下载文件,然后从 100 开始它会打开一个弹出窗口另存为".因此,我想要么根本不下载该文件,要么将其移动到回收站"中的特定文件夹中.

The issue I got is that, it starts to download the file each time I open a link with name file(1) file(2) .... file(99) then starting at 100 it opens a popup window "Save As". So I would like to either don't download the file at all or be able to move it in a specific folder in the "Recycle Bin".

如何找到可以与 add_argument 和 add_argument 一起使用的选项?我试图查看 Chrome://about/,但看不到直接对应关系.

How do I find which options could be I used with add_argument and add_argument? I tried to look at Chrome://about/ but I couldn't see a direct correspondence.

非常感谢.

干杯.

法比安.

推荐答案

您为默认目录声明的路径无效.转义反斜杠或提供文字字符串.

The path you declared for the default directory is invalid. Either escape the back slashes or provide a literal string.

options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:UsersxxxdownloadsTest",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})
driver = webdriver.Chrome(chrome_options=options)

以下是可用的首选项:

https://cs.chromium.org/chromium/src/chrome/common/pref_names.cc

这篇关于如何在 Chrome 中使用 Selenium + Python 绑定控制文件的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆