Вы можете установить каталог по умолчанию для загрузки файлов при инициализации chromedriver:
import os
from selenium import webdriver
# Set the target subfolder based on the current working directory
dl_folder = '/downloads'
dl_location = os.path.join(os.getcwd(), dl_folder)
# Add the headless argument
chrome_options.add_argument('headless')
# Prepare a dict with additional preferences. This is where the magic happens:
prefs = {"download.default_directory": dl_location}
chrome_options.add_experimental_option("prefs", prefs)
# launch the driver
driver = webdriver.Chrome(executable_path= '/path/to/executable', chrome_options= chrome_options)
После этого вы нажимаете что-то, что инициализирует загрузку
exportbtn = driver.find_element_by_id('exporter-csv')
exportbtn.click()
И затем вы получаете последнююдобавлен файл из папки.Например, если это CSV, загрузите его в фрейм данных:
import glob
import pandas as pd
list_of_files = glob.glob(dl_location+ '/*')
latest_file = max(list_of_files, key=os.path.getctime)
df = pd.read_csv(latest_file)
Я не знаю, как напрямую получить имя файла или файл.Если есть что-нибудь, мне любопытно услышать об этом.