pythonselenium-webdriverseleniummanager

force selenium manager to download browser in python


I want to force the selenium manager to download a browser instead of using any locally installed one. According to the doc that option exists. But I really don't understand when I should define the option in the code.

The most obvious option does not seem to work.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

opts = Options()
opts.add_argument("--force-browser-download")

driver = webdriver.Firefox(options=opts)

driver.get("http://www.python.org")
driver.close()

Or is that entirely wrong and these options are not supposed to be defined in the code?

Using Selenium v4.20.0


Solution

  • So the reason I wanted to force a browser download is because I wanted to avoid the whole "binary is not a Firefox executable" bug that seems to be caused by firefox being locally installed from snap or something.

    I thought, forcing a download would make it ignore any locally installed version and thus avoid the bug.

    And actually setting the option through ENVv seems to have worked with a little tweak, thanks JeffC

    Adding this to the beginning seems to avoid the bug.

    import os
    os.environ["SE_FORCE_BROWSER_DOWNLOAD"] = "true"
    os.environ["SE_CACHE_PATH"] = "./.cache"
    

    I had to add a custom cache path, because without it, it does not seem to work.

    Honestly, doesn't make much sense to me, why that happens.

    Because the cache path just changes where stuff is downloaded too. But the same files are downloaded into cache assuming the force download is enabled. And in the logs it references the default cache path and the custom cache path respectivly as the Browser path: ./.cache/firefox/linux64/125.0.3/firefox

    Yet for some reason, the bug happens when I leave the default path which means it is using the local snap-installed firefox install regardless what the log says.

    And when I use a custom path it actually executes the downloaded version.

    Very confused as to why this would happen, but it works now so ¯\_(ツ)_/¯