首页 > 编程问答 >无法连接到127.0.0.1:53233的chrome,此版本的ChromeDriver仅支持Chrome版本114。当前浏览器版本是127.0.6533.72

无法连接到127.0.0.1:53233的chrome,此版本的ChromeDriver仅支持Chrome版本114。当前浏览器版本是127.0.6533.72

时间:2024-07-27 15:03:11浏览次数:9  
标签:python docker selenium-webdriver undetected-chromedriver

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 版本,从而避免版本不匹配的问题。

标签:python,docker,selenium-webdriver,undetected-chromedriver
From: 78791988

相关文章

  • 【Python】利用 face_recognition 库进行人脸检测识别【附完整示例】
    1.背景条件1.1安装所需库首先安装face_recognition和Pillow这两个库。您可以使用以下命令来安装它们:pipinstallface_recognitionPillow-ihttps://pypi.tuna.tsinghua.edu.cn/simple1.2拷贝代码安装完成后,您就可以在本地运行以下提供的代码了。importfac......
  • 太强了,Python+Excel真的是神仙组合!
    本书是由流行开源Python库xlwings的创始人:费利克斯·朱姆斯坦(FelixZumstein)所撰写。他详细阐述了如何将Python与Excel结合使用,让任务自动化,从而实现效率飞跃。为了帮助初学者克服对Python的恐惧,作者特意将教程内容设计成从简单到复杂的顺序进行介绍。这本书PDF共282页,分为4个......
  • 在 Python 中获取精确的 Android GPS 位置
    我尝试在Python中获取Android手机的GPS位置(使用QPython3应用程序)。这种可行,但是Android中似乎有几个LocationProvider:gps:纯gps定位,速度慢,耗能,但非常准确,正是我所需要的。网络:GPS和wifi/小区定位的混合,更快,但不太准确被动......
  • Dockers 部署Nodejs环境的ts 项目遇到的问题
    Dockers部署Nodejs环境的ts项目遇到的问题由于不熟悉TS和nodejs所以过程比较曲折。webpack.config\tsconfig.json\package.json\是几个比较关键的文件。细节可以去看详细文档,这里不展开讲;主要是缺失了start.sh文件和执行npmrunbuild的时候报错问题;遇到报错时,请用最新的AI,......
  • 使用 docker run 将 Python 单击选项传递给 ENTRYPOINT 会出现错误:“在 $PATH 中找不
    我有一个简单的python脚本,我想在docker容器内运行它。它打印一行消息“Hello{name}”。python脚本使用clickCLI界面来定义收件人名称,如果我直接运行它(不使用dockerrun命令),它将如下所示:pythonhello.py-nSmithDockerbuild命令:dockerbuild.-thello:1.......
  • 标题:在 OpenSees Python 中定义具有特定卸载行为的双线性弹塑性材料
    我正在使用Python中的OpenSees,我想定义一种在负载下表现出双线性弹塑性行为的材料。但是,我需要在卸载过程中将材质返回到其原始位置,遵循准确的加载路径。在此处输入图像描述我不确定如何在OpenSees中正确实现卸载行为,我正在寻找实现这一具体材料反应的指导。......
  • 使用正则表达式删除Python中常见的公司名称后缀
    我正在努力删除一些公司名称中的后缀。预期结果如下:原始名称:AppleInc.SonyCorporationFiatChryslerAutomobilesS.p.A.SamsungElectronicsCo.,Ltd.清除名称:AppleSonyFiatChryslerAutomobilesSamsungElectronics到目前为止我所做的:importred......
  • 如何将 Brave 网络浏览器与 python、selenium 和 chromedriver 结合使用?
    我从Google的Chrome切换到Brave网络浏览器并且很难让它像Chrome一样与Brave一起使用。Brave是基于Chromium的,所以我猜它应该不会那么难。我确保我的Brave和Chromedriver处于相同版本,像这样,~/some/path$chromedriver--versionChromeDriver76.0.3......
  • 覆盖 python 应用程序时权限被拒绝
    我使用python制作了一个粗略的自动更新应用程序,并使用freeze-cx制作了exe文件。首先,该应用程序检查firebase服务器上是否有最新版本的文件可用,如果可用则下载zip文件。并且应用程序解压并覆盖文件。this_file_path=sys.executableifgetattr(sys,'frozen......
  • Python数据分析案例55——基于LSTM结构自编码器的多变量时间序列异常值监测
    案例背景时间序列的异常值检测是方兴未艾的话题。比如很多单变量的,一条风速,一条用电量这种做时间序列异常值检测,想查看一下哪个时间点的用电量异常。多变量时间序列由不同变量随时间变化的序列组成,这些时间序列在实际应用中通常来自不同的传感器或数据源。多变量时间序列异......