Uncategorized

Can’t load existing chrome profile (python + selenium + docker)


Struggle with subject for two days: I have chrome profile with cookies for some site, that loads by Selenium perfectly without Docker. But when i dockerise my app, all cookies in chrome profile get lost.
Is there any trick to load data correctly?

dockerfile

FROM python:3.8-slim as builder

# This flag is important to output python logs correctly in docker!
ENV PYTHONUNBUFFERED 1
# Flag to optimize container size a bit by removing runtime python cache
ENV PYTHONDONTWRITEBYTECODE 1

ENV TZ=Europe/Moscow
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install --no-install-recommends -y \
    fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 \
    libnspr4 libnss3 libgbm1 lsb-release xdg-utils libvulkan1 libxss1 libdbus-glib-1-2 \
    curl unzip wget bzip2 \
    xvfb xauth libgl1-mesa-dev xz-utils && \
    apt-get clean && apt-get -y autoremove && \
    rm -rf /var/lib/apt/lists/*

RUN wget http://archive.ubuntu.com/ubuntu/pool/main/libu/libu2f-host/libu2f-udev_1.1.4-1_all.deb && \
    dpkg -i libu2f-udev_1.1.4-1_all.deb

RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    wget --no-verbose https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip chromedriver_linux64.zip -d /usr/bin && \
    chmod +x /usr/bin/chromedriver && \
    rm chromedriver_linux64.zip && \
    \
    CHROME_SETUP=google-chrome.deb && \
    wget --no-verbose -O $CHROME_SETUP "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb" && \
    dpkg -i $CHROME_SETUP && \
    apt-get install -y -f --no-install-recommends && \
    rm $CHROME_SETUP

COPY requirements.txt .
RUN pip3 install -r requirements.txt
RUN rm requirements.txt
COPY . .

WORKDIR tst_docker

CMD ["python3", "main.py"]

docker-compose.yaml

version: '3.9'
services:
  tst_docker:
    user: root
    container_name: "tst_docker"
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - ./profile1:/profile1
      - .:/tst_docker
    env_file:
      .env
    restart: no
    network_mode: host
    init: true

main.py example

import time

from selenium.webdriver import ChromeOptions
from seleniumwire.webdriver import Chrome
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager


def set_options():
    options = ChromeOptions()

    options.add_argument("start-maximized")
    options.add_argument("enable-automation")
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-browser-side-navigation")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1920,1080")
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--allow-running-insecure-content')

    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument(fr'user-data-dir=/profile1')

    return options


options = set_options()

with Chrome(service=ChromeService(ChromeDriverManager().install()),
            options=options) as driver:
    driver.get('https://www.google.com/')
    time.sleep(10)
    driver.save_screenshot(f'tst1.png')

Structure of the project:
enter image description here

If i use this code sample without docker in Windows (example main.py is the same, but changes path to the profile directory to absolute), profile and cookies loads. When i ran docker cookies cleans in profile1 directory, so when i try load profile in Windows, cookies not load.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *