如何在Chrome中使用Selenium + Python绑定控制文件的下载 [英] How to control the download of files with Selenium + Python bindings in Chrome
问题描述
在哪里可以找到描述可以与Selenium和Chrome网络浏览器一起使用的选项的文档?我想在网络浏览器中打开一个链接(以获取证书),但不想下载相应的文件(.pdf或.tiff或.jpeg).我在Windows 7笔记本电脑上使用Python 2.7,硒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:\Users\xxx\downloads\Test"}) # 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:\Users\xxx\downloads\Test"})
对我不起作用.
相同于:
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.
非常感谢.
干杯.
Fabien.
推荐答案
您为默认目录声明的路径无效.要么转义反斜杠,要么提供文字字符串.
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:\Users\xxx\downloads\Test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome(chrome_options=options)
以下是可用的偏好设置:
Here are the available preferences:
https://cs.chromium.org/chromium/src/chrome/common/pref_names.cc
这篇关于如何在Chrome中使用Selenium + Python绑定控制文件的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!