Last active
November 4, 2024 09:02
-
-
Save lyf2000/c48ec2b3d2f0f2cc93dd6566b6e5811d to your computer and use it in GitHub Desktop.
Install google chrome to use with docker python:3.12.3-bookworm selenium==4.22.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
app: | |
build: | |
context: . | |
dockerfile: ./python.selenium.Dockerfile | |
command: python main.py | |
depends_on: | |
- selenium | |
selenium: | |
image: selenium/standalone-chrome | |
platform: linux/amd64 # for mac m chips | |
volumes: | |
- /dev/shm:/dev/shm # solves low mem in container. but may occur low speed of work | |
- chrome_profiles:/tmp/chrome # to cache profile(sessions, logins...) | |
ports: | |
- 4444:4444 | |
volumes: | |
chrome_profiles: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Taken and modified from https://tproger.ru/articles/bystro-i-prosto-razvorachivaem-prilozhenie-na-selenium-python-v-docker | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
def test_fullpage_screenshot(url: str, filename: str): | |
options = webdriver.ChromeOptions() | |
options.add_argument("--no-sandbox") | |
options.add_argument("--window-size=1920,1080") | |
options.add_argument("--headless") | |
options.add_argument("--disable-gpu") | |
options.add_argument("--user-data-dir=/tmp/chrome") | |
driver = webdriver.Chrome(options=options) | |
try: | |
driver.get(url) | |
time.sleep(2) | |
ele = driver.find_element(By.TAG_NAME, "body") | |
height = driver.execute_script("return document.body.scrollHeight") | |
driver.set_window_size(1920, height) | |
driver.save_screenshot(f"screenshots/{filename}") | |
except Exception: | |
pass | |
finally: | |
driver.close() | |
driver.quit() | |
if __name__ == "__main__": | |
test_fullpage_screenshot("https://tproger.ru", "tproger.png") | |
test_fullpage_screenshot("https://stackoverflow.com", "stackoverflow.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.12.3-bookworm | |
ENV PYTHONUNBUFFERED 1 | |
ENV PYTHONDONTWRITEBYTECODE 1 | |
# custom, needed for other reqs | |
RUN apt-get update \ | |
# dependencies for building Python packages | |
&& apt-get install -y build-essential \ | |
# psycopg2 dependencies | |
&& apt-get install -y libpq-dev \ | |
# WeasyPrint | |
&& apt-get install -y libpango-1.0-0 libpangoft2-1.0-0 \ | |
# cleaning up unused files | |
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ | |
&& rm -rf /var/lib/apt/lists/* | |
COPY ./reqs.txt /reqs.txt | |
RUN pip install -r /reqs.txt | |
WORKDIR /app | |
COPY . /app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment