Chrome does not use my download.default_directory settings, and always downloads to my Python execution folder

Question:

This is Chrome 111 on Debian 11 – I am attempting to download a file to a folder. As of 3 AM last night, it was working – as of 6 AM this morning, with no server modifications or updates or resets, it was not – all files any Python script utilizing this code segment now download the files to their execution directory. Below is the Chrome selenium headless browser driver instantiation:

def create_temp_folder():
    temp_folder = new_file_folder+directory_separator+(str)(uuid.uuid4())
    os.mkdir(temp_folder)
    return temp_folder


def init_chrome_service(temp_directory = False):
    #init stage, notes for later class construction
    service = Service(executable_path=ChromeDriverManager().install())
    chrome_options = Options()
    if temp_directory:
        download_directory = create_temp_folder()

    chrome_options.add_argument("--disable-web-security")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless")

    prefs = {'download.default_directory' : download_directory} 
    chrome_options.add_experimental_option("detach", True)
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(options=chrome_options) 
    return driver, download_directory

The chrome.default_directory variable exists, is being set, is properly creating the directory, has permissions, hasn’t randomly lost permissions, and the behavior is the same whether I run the Python script as sudo, myself, or through a cron job. I have tried reinstalling chrome and the chromedriver.

What’s even stranger is, the exact same copy of the code on my Windows computer works perfectly – so something changed in the Debian environment, is my working theory, but I cannot for the life of me isolate what.

The code that actually downloads the file is trivial – a driver.get(‘elementid’).click() on a button that runs a report.

Asked By: Kwahn

||

Answers:

Headless mode started behaving differently a couple of days ago. If you updated Chrome in your environment you may have a build with the issue.

https://bugs.chromium.org/p/chromedriver/issues/detail?id=4357

Answered By: Chris S

Having the same issue, the following suggestion worked: switch from options.add_argument(‘–headless’) to options.add_argument(‘–headless=new’).

Answered By: Misha Voit

I had the same issue and used the above code, –headless=new, and it resolved my path issues. This is what my code look like:

chromeOptions = Options()
prefs = {"download.default_directory": download_dir, "directory_upgrade": True,  "extensions_to_open": ""} 
chromeOptions.add_argument('--headless=new')
chromeOptions.add_argument('--no-sandbox')
Answered By: smaups