skip to Main Content
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>

I think the issue arises, because docker can’t use the chromedriver installed in Dokcerfile, i’m open to any other approacahes, of course. Before these configurations, kept getting chromedriver.exe doesn’t exist error in Docker. After fixing that, encountered this issue. Would really appreciate if you can give me a hand with this problem, have a nice day.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot for your help, i actually solved the issue using ChromeDriverManager. Here is the extra code i have applied:

    def install_driver(self) -> None:
        ChromeDriverManager().install()
        os.chmod("/root/.wdm/drivers/chromedriver/linux64/127.0.6533.72/chromedriver-linux64/chromedriver", 0o755)
    
    .....
    def get_driver(self):
        options = self.get_options()
        self.install_driver()
    
        self.driver = uc.Chrome(options=options, driver_executable_path="/root/.wdm/drivers/chromedriver/linux64/127.0.6533.72/chromedriver-linux64/chromedriver", headless = False,
                                use_subprocess = False)
    

    It worked just fine for Docker. You can see the path Chrome Driver Manager installs chromedriver by printing or logging (in Docker) the value it returns by something like this:

    def get_path(self) -> str:
        path = ChromeDriverManager().install()
        return path
    
    print(get_path)
    

    The chromedriver is in the same directory as THIRD_PARTY_NOTICES.chromedriver path it returns. For example in windows it returns:

    C:Usershikme.wdmdriverschromedriverwin64127.0.6533.72chromedriver-win32/THIRD_PARTY_NOTICES.chromedriver
    

    You need to convert it into something like this:

    C:Usershikme.wdmdriverschromedriverwin64127.0.6533.72chromedriver- 
    win32/chromedriver
    

    On the other hand, for docker the path, it will be the one i have written in the first code.


  2. Thats work for me

    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 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,
    , headless = False, use_subprocess = False,version_main = 127)
            self.set_geolocation(40.4093, 49.8671, 100)    
            return self.driver
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search