admin管理员组

文章数量:1535103

selenium之文件下载

WebDriver允许我们设置默认的文件下载路径,也就是说,文件会自动下载并且存放到设置的目录中,下边以chrome浏览器为例,自动化代码如下:
chrome浏览器文件下载

from selenium import webdriver

options = webdriver.ChromeOptions()
#TODO 'profile.default_content_setting.popups':0 设置为0表示禁止弹出下载窗口
#TODO  'download.default_directory':"E:\\dir" 修改下载地址为E:\\dir
prefs = {'profile.default_content_setting.popups':0,
         'download.default_directory':"E:\\dir"}
# 'download.default_directory':os.getcwd() 设置文件下载路径,使用os.getcwd()方法获取当前脚本作为下载文件的保存位置
# prefs = {'profile.default_content_setting.popups':0,
#          'download.default_directory':os.getcwd()}

options.add_experimental_option('prefs',prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://pypi/project/selenium/#files")
driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click()

本文标签: 浏览器文件selenium