webdriver (xiii) --- Download

 Firefox browser:

import os
from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "binary/octet-stream")

driver = webdriver.Firefox(firefox_profile=fp)
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click()
browser.download.folderList set to 0 indicates that the file is downloaded to the browser's default download path, and 2 means downloaded to the specified directory
browser.download.dir used to specify the directory to download the file. Obtain the location of the current file by os.getcwd () method, that is, save the downloaded files. 
Specify the type of file you want to download that Content-type value
by "binary / octet-stream" represents a binary

Chrome browser:
import os
from selenium import webdriver

options = webdriver.ChromeOptions() 
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()
download.default_directory settings file download directory

Guess you like

Origin www.cnblogs.com/xxxyang/p/11938003.html