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

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

问题描述

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

 #Chrome浏览器
选项= webdriver.ChromeOptions()
options.add_argument(' - ignore-certificate-errors')
#options.add_argument(' - disable-download-通知')#似乎不工作
#options.add_experimental_option(prefs,{download.default_directory,C:\ Users \\\\\\\\\\\\\\\\\\\\\\\\'))#doesn '工作
#options.add_experimental_option(prefs,{download.prompt_for_download:False})#似乎不工作
#options.add_experimental_option(prefs,{'profile。 default_content_settings':{'images':2}})#这将禁用浏览器中的图片加载
options.add_argument(user-agent =+ user_agent_profile)
driver_main = webdriver.Chrome(chrome_options =选项)

#打开Web应用程序portail
driver_main.get(https:// my_link)

我在这个主题上发现了很多讨论,但是没有一个解决方案可行。例如:

  add_experimental_option(prefs,{download.default_directory,C:\Users\xxx\\ \\下载\测试})

不适用于我



相同:

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

(我也尝试使用false)

while:

  add_argument(user-agent =+ user_agent_profile)


>

似乎可行!



我不确定明白什么是错的

p>

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



如何查找哪些选项可以用于add_argument和add_argument?我尝试着看看Chrome:// about /但我看不到直接的信件。



非常感谢



干杯



Fabien

解决方案

默认目录无效。或者提供一个字符串。

  options = webdriver.ChromeOptions()
options.add_experimental_option( prefs,{
download.default_directory:rC:\Users\xxx\downloads\Test,
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


Where can I find the documentation that describe 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"})

doesn't work for me

same for:

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

(I also try with "false")

while:

add_argument("user-agent="+user_agent_profile)

seems to work !

I am not sure to understand what is wrong

The issue I got is that, it start to download the file each time I open a link with name file(1) file(2) .... file(99) then starting at 100 it open a popup windows "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".

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

Thanks a lot

Cheers

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屋!

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