driver code:
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import undetected_chromedriver2 as uc
from undetected_chromedriver2.options import ChromeOptions
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
import pickle
from bs4 import BeautifulSoup
from typing import List
from random import uniform
class Driver:
def get_options(self):
options = ChromeOptions()
options.headless = False
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.debugger_address = "127.0.0.1:53233"
options.add_argument("--disable-gpu")
options.add_argument('--remote-allow-origins=*')
options.add_argument('--remote-debugging-port=53233')
options.add_argument("--lang=az-AZ")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-popup-blocking")
options.add_argument('--headless=new')
options.add_argument("--disable-blink-features=AutomationControlled")
return options
def get_service(self):
service = Service(ChromeDriverManager(driver_version = "127.0.6533.72").install())
return service
def set_geolocation(self, latitude, longitude, accuracy):
self.driver.execute_cdp_cmd(
"Page.setGeolocationOverride",
{
"latitude": latitude,
"longitude": longitude,
"accuracy": accuracy
}
)
def get_driver(self):
options = self.get_options()
self.driver = uc.Chrome(options = options, driver_executable_path = "/usr/local/bin/chromedriver", headless = False, use_subprocess = False)
self.set_geolocation(40.4093, 49.8671, 100)
return self.driver
Dockerfile:
# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Install required packages and dependencies
RUN apt-get update && apt-get install -y \
wget \
unzip \
curl \
gnupg2 \
&& wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update \
&& apt-get install -y google-chrome-stable \
&& rm -rf /var/lib/apt/lists/*
# Install ChromeDriver
RUN CHROMEDRIVER_VERSION=$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ && \
rm /tmp/chromedriver.zip
# Set the environment variable for the ChromeDriver binary
ENV PATH="/usr/local/bin:${PATH}"
# Copy the requirements file
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY . /app
# Set the working directory
WORKDIR /app
# Set the entry point for the container
CMD ["python", "main.py"]
docker-compose.yml:
version: '3.8'
services:
web:
build: .
command: python main.py
platform: linux/amd64
volumes:
- .:/usr/src/app
ports:
- "8000:80"
env_file:
- .database_env
Full error:
WARNING:uc:could not detect version_main.therefore, we are assuming it is chrome 108 or higher
Traceback (most recent call last):
File "/app/main.py", line 77, in <module>
asyncio.run(main())
File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/app/main.py", line 30, in main
driver = Driver().get_driver()
File "/app/get_explore_data.py", line 60, in get_driver
self.driver = uc.Chrome(options = options, driver_executable_path = "/usr/local/bin/chromedriver", headless = False, use_subprocess = False)
File "/usr/local/lib/python3.9/site-packages/undetected_chromedriver2/__init__.py", line 453, in __init__
super(Chrome, self).__init__(
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 212, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.9/site-packages/undetected_chromedriver2/__init__.py", line 711, in start_session
super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 299, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:53233
from session not created: This version of ChromeDriver only supports Chrome version 114
Current browser version is 127.0.6533.72
Stacktrace:
#0 0x561f04dd84e3 <unknown>
#1 0x561f04b07c76 <unknown>
#2 0x561f04b3504a <unknown>
#3 0x561f04b2c884 <unknown>
#4 0x561f04b6bccc <unknown>
#5 0x561f04b6b47f <unknown>
#6 0x561f04b62de3 <unknown>
#7 0x561f04b382dd <unknown>
#8 0x561f04b3934e <unknown>
#9 0x561f04d983e4 <unknown>
#10 0x561f04d9c3d7 <unknown>
#11 0x561f04da6b20 <unknown>
#12 0x561f04d9d023 <unknown>
#13 0x561f04d6b1aa <unknown>
#14 0x561f04dc16b8 <unknown>
#15 0x561f04dc1847 <unknown>
#16 0x561f04dd1243 <unknown>
#17 0x7f8398dd8134 <unknown>
我认为问题出现了,因为 docker 无法使用 Dokcerfile 中安装的 chromedriver,当然,我对任何其他方法持开放态度。在进行这些配置之前,Docker 中不断出现 chromedriver.exe 不存在的错误。修复后又遇到这个问题。如果您能帮助我解决这个问题,我将非常感激,祝您有美好的一天。
遇到的问题是 ChromeDriver 和 Chrome 浏览器版本不匹配。的代码试图使用 ChromeDriver 114 来控制 Chrome 127,这会导致兼容性问题。
解决这个问题的最佳方法是确保 ChromeDriver 和 Chrome 浏览器版本一致。以下是如何在 Docker 中实现这一点:
1. 更新 Dockerfile 中的 ChromeDriver 安装步骤:
不要在 Dockerfile 中指定具体的 ChromeDriver 版本,而是使用
webdriver-manager
自动下载与安装的 Chrome 浏览器版本匹配的 ChromeDriver 版本。
# ... 其他 Dockerfile 内容 ...
# 安装 Python 依赖
RUN pip install --no-cache-dir -r requirements.txt
# 将应用程序代码复制到容器中
COPY . /app
# 设置工作目录
WORKDIR /app
# 在容器启动时更新 ChromeDriver
RUN webdriver-manager update --gecko false --edge false
# 确保 webdriver-manager 将 ChromeDriver 安装到 PATH 中
ENV PATH="/root/.wdm/chromedriver/linux64/:$PATH"
# 设置容器启动命令
CMD ["python", "main.py"]
2. 修改 Python 代码:
不需要在 Python 代码中指定 ChromeDriver 的路径,因为
webdriver-manager
已经将其添加到
PATH
环境变量中。
class Driver:
# ... 其他代码 ...
def get_driver(self):
options = self.get_options()
self.driver = uc.Chrome(options=options, headless=False, use_subprocess=False)
self.set_geolocation(40.4093, 49.8671, 100)
return self.driver
3. 重新构建并运行 Docker 镜像:
docker-compose build
docker-compose up -d
这样,每次构建 Docker 镜像时,
webdriver-manager
都会自动下载与当前 Chrome 浏览器版本匹配的 ChromeDriver 版本,从而避免版本不匹配的问题。