2.下载文件
(1)firefox浏览器下载文件
browser.download.folderList
0:默认下载路径
2:下载到指定目录
import os
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.downloadList", 2) # 下载到指定目录
fp.set_preference("browser.download.dir", os.getcwd()) # os.getcwd()获取当前文件的所在位置,即下载文件保存的目录
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "binary/octet-stream") # 指定要下载的问你就类型,binary/octet-stream二进制类型
driver = webdriver.Firefox(firefox_profile=fp) # 作为参数传给Firefox()
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click()
(2)Chrome浏览器下载文件
import os
from selenium import webdriver
options = webdriver.ChromeOptions()
# 0禁止弹出下载窗口
# download.default_directory设置下载路径
prefs = {
"profile.default_content_settings.popups": 0,
"download.default_directory": os.getcwd()}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click()