Selenium и Chromedriver

Amverum Cloud uses containers under the hood. To be able to use selenium in a container, you need to install the necessary libraries and specify the display port in this container.

Using a ready-made image

This image already has the ChromeDriver and Chrome we need

Installation steps:

  1. Create a Dockerfile in the project directory.

  2. In the Dockerfile, specify the image with

    FROM joyzoursky/python-chromedriver:3.9
    
  3. We assign a WORKDIR and specify the application files that will be included in the final image (all files in the project folder):

    WORKDIR  /app
    COPY  .  /app
    
  4. Add a command to install dependencies (we will specify them a little later):

    RUN  pip  install  -r  requirements.txt
    
  5. We set the command to launch the application:

    CMD ["python", "main.ru"]
    

The resulting Dockerfile:

FROM  joyzoursky/python-chromedriver:3.9
WORKDIR  /app
COPY  .  /app
RUN  pip  install  -r  requirements.txt
CMD  ["python",  "main.ru"]

Note

Instead of version 3.9 you can specify another Python version that you need and that is supported by the image author.

Creating your own image with ChromeDriver

If the required Python version is not supported, you can create your own image with the required version by installing the necessary drivers into it.

In the example given, the base image is python image:3.8 which can be replaced with a suitable one.

# set the base image (instead of 3.8, you can specify another version of Python)
FROM python:3.8

# we are installing google-chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# set the desired display port
ENV DISPLAY=:99

Example

The example will output the Title of the page www.python.org

Add the following files to the root of the project:

  • requirements.txt

    selenium==4.17.2
    
  • amverum.yml

    meta:
      environment: docker
      toolchain:
        name: docker
        version: latest
    build:
      dockerfile: Dockerfile
      skip: false
    run:
      persistenceMount: /data
      containerPort: 80
    
  • Dockerfile

    FROM  joyzoursky/python-chromedriver:3.9
    WORKDIR  /app
    COPY  .  /app
    RUN  pip  install  -r  requirements.txt
    CMD  ["python",  "main.ru"]
    
  • main.ru

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://www.python.org")
    print(driver.title)
    
    driver.close()
    

If the program works correctly, the following text will be displayed in the console: Welcome to Python.org