Skip to content

Docker + Docker-compose + Native + Selenium workspace to build any selenium app

Notifications You must be signed in to change notification settings

supermavster/docker-selenium-python

Repository files navigation

Docker + Docker-compose + Selenium + Python

This is a simple example of how to use Docker, Docker-compose and Selenium together to run a simple Python script.

In this case, we can use three different ways to run the script:

  1. Native
  2. Dockerfile
  3. Docker-compose with Simulated Web Driver

Native

To run the script natively, we can use the next commands.

  1. You need to install Chrome or Firefox.

  2. Start the workspace.

    virtualenv venv
    source venv/bin/activate
  3. Edit the file .env to the workspace.

    As example, can be source/.env

    ENVIRONMENT=local
    #ENVIRONMENT=docker
    #ENVIRONMENT=remote
    
    PATH_ASSETS=assets
    
    #BROWSER=chrome
    #EXTENSIONS_CHROME=["metamask"]
    
    BROWSER=firefox
    EXTENSIONS_FIREFOX=["metamask", "captcha"]
    
    REMOTE_URL=http://selenium-hub:4444/wd/hub
    #REMOTE_URL=http://0.0.0.0:4444/wd/hub
    #REMOTE_URL=http://localhost:4444/wd/hub
    
    METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
  4. Inside to the folder source

    cd source 
  5. Install the dependencies.

    python -m pip install --upgrade pip
    pip install -r requirements.txt
  6. Execute the script.

    python main.py
  7. Stop the workspace.

    deactivate

Dockerfile

  1. Edit the file .env to the workspace.

    As example, can be source/.env

    #ENVIRONMENT=local
    ENVIRONMENT=docker
    #ENVIRONMENT=remote
    
    PATH_ASSETS=assets
    
    BROWSER=chrome
    EXTENSIONS_CHROME=["metamask", "captcha"]
    
    #BROWSER=firefox
    #EXTENSIONS_FIREFOX=["metamask"]
    
    REMOTE_URL=http://selenium-hub:4444/wd/hub
    #REMOTE_URL=http://0.0.0.0:4444/wd/hub
    #REMOTE_URL=http://localhost:4444/wd/hub
    
    METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
    
  2. Build the Docker image.

    docker build -f docker/single/Dockerfile -t selenium-python-docker .
  3. Run the Docker image.

    docker run -it --rm -v $(pwd)/source:/usr/src/app -w /usr/src/app --env-file=.env selenium-python-docker

Docker-compose

  1. Edit the file .env to the workspace.

    As example, can be source/.env

    #ENVIRONMENT=local
    #ENVIRONMENT=docker
    ENVIRONMENT=remote
    
    PATH_ASSETS=assets
    
    BROWSER=chrome
    EXTENSIONS_CHROME=["metamask"]
    
    #BROWSER=firefox
    #EXTENSIONS_FIREFOX=["metamask"]
    
    REMOTE_URL=http://selenium-hub:4444/wd/hub
    #REMOTE_URL=http://0.0.0.0:4444/wd/hub
    #REMOTE_URL=http://localhost:4444/wd/hub
    
    METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
    
  2. Build the Docker image.

    docker-compose build
  3. Up the Docker image.

    docker-compose up
  4. Execute the script in docker service.

    docker-compose exec -it app bash

    Or execute the script in the container.

    docker exec -it $(docker ps | grep app | awk '{print $1}') bash
    # python main.py
  5. In the localhost:4000, you can see the result in session, note: the password of session vnc is secret

  6. Stop the Docker image.

    docker-compose down

About

Docker + Docker-compose + Native + Selenium workspace to build any selenium app

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published