Search code examples
pythonseleniumselenium-chromedriveralpine-linux

Selenium run on alpine 3.6 container


I'm trying to run Selenium on alpine 3.6 container (FROM alpine:3.6).

What I'm trying in container shell:

apk update
apk add python3
pip3 install -U selenium
apk add chromium
apk add chromium-driver

And running the following python (using python3):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'usr/bin/chromedriver') # Thrown an exception

And got the following exception:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed (Driver info: chromedriver=2.27 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 5.0.0-23-generic x86_64)

selenium=3.141.0
chromium=57.0.2987.133
chromeDriver=2.27

How can I solve it?


Solution

  • Solved by the followings steps (Work with alpine3.6):

    Update the repositores:

    echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" > /etc/apk/repositories
    echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
    

    Apk Update:

    apk update
    

    Install chromium & chromedriver:

    apk add chromium
    apk add chromium-chromedriver
    

    Install python3, selenium:

    apk add python3
    pip3 install -U selenium
    

    And the following python code works for me:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(options=chrome_options)
    driver.get('http://example.com')