selenium指定目录下载文件且阻止浏览器弹框该如何配置呢?
网上绝大部分的帖子的配置都是不起作用的。目前用的谷歌浏览器80版本的,经过测试以下方式完全可以实现该功能。只把核心代码展示下
代码语言:javascript复制 start_time = time.time()
today = time.strftime('%Y-%m-%d', time.localtime(start_time))
save_path = r"E:下载-账户{0}".format(today) # 指定下载保存位置
# 记录失败
f = open('export_fail_zhanghu{0}.txt'.format(today),'w',encoding='utf-8')
start,end = get_dates('date.txt')
city_urls = get_city_urls('city_id_url_zhanghu.txt',start,end)
cookie = get_cookie('cookie.txt')
Options = Options()
# Options.add_argument("--headless")
Options.add_experimental_option("prefs", {
"download.default_directory": save_path,
"download.prompt_for_download": False, # 不弹框
})
driver = webdriver.Chrome(chrome_options=Options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath':save_path}}
command_result = driver.execute("send_command", params)
run()
f.flush()
f.close()
end_time = time.time()
print('耗时{0}min'.format((end_time-start_time)/60))
selenium指定目录下载文件且阻止浏览器弹框配置如上,请放心使用。