selenium设置(有界面/无界面浏览器)下载文件路径

配置下载文件路径

配置方法是添加download.default_directory,如:

option = webdriver.ChromeOptions()
prefs = {
    
    
	'download.default_directory': r"E:\xxxx\xxxx",  # 设置默认下载路径
	"profile.default_content_setting_values.automatic_downloads": 1  # 允许多文件下载
}
option.add_experimental_option("prefs", prefs)
webdriver.Chrome(chrome_options=option)

无界面浏览器设置路径

对于无界面浏览器,为了安全,默认是不允许在无界面下进行下载文件到本地的操作的,但是可以通过配置进行修改,详细请参考:https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium

option = webdriver.ChromeOptions()
option.add_argument("--incognito")  # 配置隐私模式
option.add_argument("--headless=new")
prefs = {
    
    
	"profile.managed_default_content_settings.images": 2,  # 禁止加载图片
	'download.default_directory': r"E:\xxxx\xxxx",  # 设置默认下载路径
	"profile.default_content_setting_values.automatic_downloads": 1  # 允许多文件下载
}
option.add_experimental_option("prefs", prefs)

猜你喜欢

转载自blog.csdn.net/weixin_35757704/article/details/130151499