添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I am using selenium in Python, which requires the chromedriver and chrome binary to be installed on the server.

How can I accomplish this?

Here is what I have so far, but on the server I see error:
“Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn’t exist) (The process started from chrome location /opt/render/project/bin/chrome/opt/google/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)”

Here is my Python file:

import os
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
load_dotenv()
# default path for homebrew-installed chromedriver
CHROMEDRIVER_PATH = os.getenv("CHROMEDRIVER_PATH", default="/usr/local/bin/chromedriver")
CHROME_BINARY_PATH = os.getenv("CHROME_BINARY_PATH") # specify path where chrome binary is installed, as necessary (see "build.sh")
HEADLESS_MODE = bool(os.getenv("HEADLESS_MODE", default="false") == "true")
def create_driver(headless=HEADLESS_MODE, chromedriver_path=CHROMEDRIVER_PATH, binary_location=CHROME_BINARY_PATH):
    options = webdriver.ChromeOptions()
    # help Mac find where you installed Chrome
    # help production server find custom installation of chrome binary (see "build.sh"):
    if binary_location:
        # https://github.com/SeleniumHQ/selenium/blob/4071737de47f1cec2dfef934f3e18a2e36db20d5/py/selenium/webdriver/chromium/options.py#L34
        options.binary_location = binary_location
    if headless:
        options.add_argument('--no-sandbox')
        options.add_argument('--incognito')
        options.add_argument('--headless')
        options.add_argument('--disable-dev-shm-usage')
    #return webdriver.Chrome(chromedriver_path, options=options)
    service = Service(executable_path=chromedriver_path)
    return webdriver.Chrome(service=service, options=options)
if __name__ == "__main__":
    driver = create_driver()
    print(driver)
    #driver.get("https://google.com")

Environment variables set on the server:

CHROME_BINARY_PATH="/opt/render/project/bin/chrome/opt/google/chrome"
CHROMEDRIVER_PATH="/opt/render/project/bin/chromedriver"
HEADLESS_MODE=true

Here is my build.sh file:

CHROME_PATH=/opt/render/project/bin/chrome/opt/google/chrome/
if [[ ! -d $CHROME_PATH ]]; then
    echo "...Downloading Chrome Binary..."
    #wget <uri> -P /path/to/folder
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P /tmp
    echo "...Installing Chrome Binary..."
    mkdir -p /opt/render/project/bin/chrome
    dpkg -x /tmp/google-chrome-stable_current_amd64.deb /opt/render/project/bin/chrome
    # this is a directory with its own "etc", "opt", and "usr" subdir
    echo "...Cleaning Up..."
    rm /tmp/google-chrome-stable_current_amd64.deb
    echo "...Adding to Path..."
    #export PATH="${PATH}:${CHROME_PATH}/opt/google/chrome/"
    export PATH="${PATH}:/opt/render/project/bin/chrome/opt/google/chrome"
  echo "...Detected Existing Chrome Binary"
#CHROMEDRIVER_PATH=/usr/local/bin/chromedriver
CHROMEDRIVER_PATH=/opt/render/project/bin/chromedriver
if [[ ! -d $CHROMEDRIVER_PATH ]]; then
    echo "...Downloading Chromedriver..."
    wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
    echo "...Installing Chromedriver..."
    unzip /tmp/chromedriver.zip chromedriver -d /opt/render/project/bin
    echo "...Cleaning Up..."
    rm /tmp/chromedriver.zip
    echo "...Adding to Path..."
    export PATH="${PATH}:${CHROMEDRIVER_PATH}"
    echo $PATH
  echo "...Detected Existing Chromedriver Installation"
echo "...Installing packages..."
pip install -r requirements.txt
echo "...Build Script Completed!"

Can you check this previous community topic: https://community.render.com/t/installing-headless-chromium-w-o-docker/5185 and let me know if that helped you installing Chrome?

Jérémy.
Render Support, UTC+3

There are two probable scenarios unfolding. Firstly, as outlined in the instructions found here: https://community.render.com/t/installing-headless-chromium-w-o-docker/5185/4, it seems that Chrome isn’t accessible via your ‘PATH’ due to the absence of an export statement in your start command.

Secondly, a disparity in major versions between your chrome and chromedriver could potentially give rise to problems.

If you desire a more in-depth investigation, kindly use the “Contact Support” form within the dashboard to get in touch.

Jérémy.
Render Support, UTC+3