Selenium stürzt sofort ab, wenn sie in Docker -Container ausgeführt werdenPython

Python-Programme
Anonymous
 Selenium stürzt sofort ab, wenn sie in Docker -Container ausgeführt werden

Post by Anonymous »

Ich begegnet auf ein Problem, wenn ich versuche, ein Selenium -Skript in einem Docker -Container auszuführen. Ich habe eine ganze Weile zurückgegangen und mit mehreren KIs Viertens und keiner konnte die Nachricht angehören, die ich an Claude gesendet habe. Jede Hilfe wäre sehr geschätzt. Versuche. < /p>
Hallo Claude!
Ich arbeite daran, einen automatisierten Webbot auszuführen, der auf einer Website Aktionen für mich ergreifen könnte.

Code: Select all

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By

# Docker path's
profilePath = "/root/.mozilla/firefox/4jqf9xwi.default-release"
firefoxPath = "/usr/bin/firefox"
firefoxDriver = "/usr/local/bin/geckodriver"

upvoteButtonPath = "/html/body/div[2]/div/div[2]/div[2]/main/div/ul/li/article/div[2]/div[2]/div[2]/div[1]/div/div[1]/button"

options = Options()
options.profile = profilePath
options.binary_location = firefoxPath
options.add_argument("--headless")
options.add_argument("--disable-gpu")  # Force software rendering
options.add_argument("--no-sandbox")  # Avoid sandboxing issues in Docker
options.add_argument("--disable-dev-shm-usage")  # Prevent crashes due to shared memory

service = Service(firefoxDriver)
driver = webdriver.Firefox(service=service, options=options)

driver.get("https://yad2.co.il/my-ads")
driver.implicitly_wait(5)

upVoteButton = driver.find_element(By.XPATH, upvoteButtonPath)
upVoteButton.click()

input("press Enter to close")

driver.quit()
< /code>
Und hier ist meine Dockerfile: < /p>
# Use an official Python runtime as a base image
FROM python:3.9-slim

# Set up environment variables for non-interactive installs
ENV DEBIAN_FRONTEND=noninteractive

# Install necessary dependencies in a single RUN command to reduce layers
RUN apt-get update && apt-get install -y \
wget \
curl \
unzip \
ca-certificates \
libx11-dev \
libxcomposite-dev \
libxrandr-dev \
libgdk-pixbuf2.0-0 \
libgtk-3-0 \
libnss3 \
libasound2 \
fonts-liberation \
libappindicator3-1 \
libxss1 \
libxtst6 \
xdg-utils \
firefox-esr \
&& apt-get clean && rm -rf /var/lib/apt/lists/*  # Clean up apt cache to reduce size

# Install GeckoDriver manually
RUN GECKO_VERSION=v0.36.0 && \
wget https://github.com/mozilla/geckodriver/releases/download/$GECKO_VERSION/geckodriver-$GECKO_VERSION-linux64.tar.gz && \
tar -xvzf geckodriver-$GECKO_VERSION-linux64.tar.gz && \
mv geckodriver /usr/local/bin/ && \
rm geckodriver-$GECKO_VERSION-linux64.tar.gz

RUN apt-get update &&  apt-get install -y \
libgtk-3-0 \
libx11-xcb1 \
libdbus-glib-1-2 \
libxt6 \
libpci3 \
xvfb

# Install Python dependencies
RUN pip install --no-cache-dir selenium

# Copy Firefox profile into the container
COPY 4jqf9xwi.default-release /root/.mozilla/firefox/4jqf9xwi.default-release/

# Set up the working directory
WORKDIR /app

# Copy the Selenium script to the container
COPY script.py /app/

# Default command to run the script
CMD ["python", "script.py"]```
< /code>
Leider stürzt es beim Ausführen des Containers sofort mit diesem Fehler ab, und egal was ich tue, kann es nicht reparieren < /p>
2025-03-04 11:34:22 Traceback (most recent call last):
2025-03-04 11:34:22   File "/app/script.py", line 29, in
2025-03-04 11:34:22     driver = webdriver.Firefox(service=service, options=options)
2025-03-04 11:34:22   File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 71, in __init__
2025-03-04 11:34:22     super().__init__(command_executor=executor, options=options)
2025-03-04 11:34:22   File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
2025-03-04 11:34:22     self.start_session(capabilities)
2025-03-04 11:34:22   File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
2025-03-04 11:34:22     response = self.execute(Command.NEW_SESSION, caps)["value"]
2025-03-04 11:34:22   File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
2025-03-04 11:34:22     self.error_handler.check_response(response)
2025-03-04 11:34:22   File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
2025-03-04 11:34:22     raise exception_class(message, screen, stacktrace)
2025-03-04 11:34:22 selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 0
2025-03-04 11:34:22
Haben Sie Einblicke in das Problem?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post