Search code examples
pythonselenium-webdriverfirefoxpyinstaller

My selenium project does not run in headless mode when I turn it into an .exe with PyInstaller


I use python 3.9. This is the code I use to activate headless mode:

c = Options()
c.headless = True
TRANSLATE_TO=''
driver = webdriver.Firefox(options=c)

When I run visual studio's debugger or when I click on the .py file it works like normal. When I turn it into an .exe it doesn't and the browser is opened (though the rest of the program runs like normal).

I should note that turning the file into an .exe with pyInstaller doesn't work great. When I try to turn the pyfile into an .exe it raises a recursion error. Then I have to add this line in the beginning of the resulting .spec file:

import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)

Once I do that, I run pyInstaller again for the .specfile and then the .exe is successfully created. (I use the 5.1 pyInstaller)

I don't know if this is relevant but the code using selenium is not from the main file that I turn into an .exe, it's from another file that the main imports and uses.


Solution

  • try this:

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options as ChromeOptions
    from selenium.webdriver.chrome.service import Service as ChromeService
    from selenium.webdriver.firefox.options import Options as FirefoxOptions
    from selenium.webdriver.firefox.service import Service as FirefoxService
    from webdriver_manager.chrome import ChromeDriverManager
    from webdriver_manager.firefox import GeckoDriverManager
    
    def initialize_chrome_driver():
        options = ChromeOptions()
        set_common_options(options)
        service = ChromeService(ChromeDriverManager().install())
        return webdriver.Chrome(service=service, options=options)
    
    def initialize_firefox_driver():
        options = FirefoxOptions()
        set_common_options(options)
        service = FirefoxService(GeckoDriverManager().install())
        return webdriver.Firefox(service=service, options=options)
    
    def set_common_options(options):
        options.add_argument("--disable-infobars")
        options.add_argument("--disable-popup-blocking")
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--log-level=3")
        options.add_argument("--disable-gpu")
        options.add_argument("--disable-extensions")
        options.add_argument('--disable-features=PrivacySandboxSettings4')
        options.add_argument("--window-size=1280,720")
        options.add_argument("--headless")
    
    # Example usage:
    # driver = initialize_chrome_driver()
    driver = initialize_firefox_driver()
    driver.get('https://www.google.com/')
    print(driver.title)
    time.sleep(100)
    driver.quit()
    

    for create exe:

    pyinstaller --noconfirm --onefile --console --icon "ICON PATH"  "PY FILE PATH"