SOLUTION!!!
Okay, the above (Update 2) explains it well, but I wanted to make a mini tutorial as it seems other’s are having this issue as well.
So you first need to all the bash script to install Chrome into Render, I used the render-build.sh script linked above and stored it in my repo. (Therefore it’s executable at ./render-build.sh
).
Note: I did modify this bash script to include my build commands at the bottom, in my case it was adding the following to the end of it: (as you’ll set in step the first bullet below)
pip install -r requirements.txt
Then I went into my Render to change the build and start commands:
- In Dashboard > Settings > Build & Deploy > Build Command, I set it to what’s blow so it’ll run the bash script and run my added build command mentioned above.
./render-build.sh
- In Dashboard > Settings > Build & Deploy > Start Command, I set it to this for FastAPI:
export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome" && uvicorn main:app --host 0.0.0.0 --port 10000
- Important Note on #2, I am using FastAPI so my I’m using
uvicorn ...
, but if you’re just running a python script than replacing everything after the&&
with the command will resolve it. So something like this:
export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome" && python main.py
As for my Python script, the code looks something like this:
# Set up the Selenium WebDriver
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--user-agent={}'.format(random.choice(list(self.user_agents))))
driver = webdriver.Chrome(options=options)
driver.set_page_load_timeout(90)
# Load the URL and get the page source
driver.implicitly_wait(6)
driver.get(url)
# ...
Lastly, it’s super important, if you want Selenium to manage your drives for you that the following criteria are met based on the Selenium Manager docs:
- The driver location isn’t specified in a
Service
class (Note: my python code reflects this) - A 3rd party driver manager is not installed (Note: if you set up something previously, remove it)
- No drivers exist in directories included in the
PATH
Environment Variable (Note: if you set up something previously remove it, note Chrome in the bash script and Chrome webdriver are different things).
Cheers