First, you need to install:- install PhantomJS.
After that change the line: driver = webdriver.Firefox() to: driver = webdriver.PhantomJS()
The rest of your code need not be changed and no browser will open.
For debugging purposes, use driver.save_screenshot('screen.png') at different steps of the code or just switch back to Firefox again:
if os.getenv("environment") == "production":
driver = webdriver.PhantomJS()
else:
driver = webdriver.Firefox()