首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >selenium 指定目录下载文件并允许多文件下载弹框

selenium 指定目录下载文件并允许多文件下载弹框

作者头像
kirin
发布于 2021-03-25 16:28:22
发布于 2021-03-25 16:28:22
2.6K1
举报
文章被收录于专栏:Kirin博客Kirin博客

  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指定目录下载文件且阻止浏览器弹框配置如上,请放心使用。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档