Search code examples
pythondockergoogle-chromeselenium-chromedriverwebdriver

How to run Chrome Headless in Docker Container with Selenium?


I am trying to run a simple test file that is meant to open google.com on chrome within an openjdk docker container and return "Completely Successfully" upon completion, however, I keep receiving the same error saying that the "service object has no attribute process". This is the error I keep receiving:

Traceback (most recent call last):
  File "/NewJersey/test.py", line 60, in <module>
    print(main())
          ^^^^^^
  File "/NewJersey/test.py", line 42, in main
    driver = webdriver.Chrome(service = service, options=chrome_options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    super().__init__(
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/chromium/webdriver.py", line 103, in __init__
    self.service.start()
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/common/service.py", line 106, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/common/service.py", line 117, in assert_process_still_running
    return_code = self.process.poll()
                  ^^^^^^^^^^^^
AttributeError: 'Service' object has no attribute 'process'

This is the code I am running:

#General Imports
from logging import error
import os
import sys
import time
import os.path
import random


#Selenium Imports (Chrome)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

#ChromeDriver Import
from webdriver_manager.chrome import ChromeDriverManager

def main():
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")

    service = ChromeService("/chromedriver")

    driver = webdriver.Chrome(service = service, options=chrome_options)

    try:
        completion_msg = reroute(driver)
        print(completion_msg)
        driver.close()
        return "Test Completed Successfully"
    
    except error as Error:
        return Error


def reroute(driver):
    driver.get("https://www.google.com")
    return "Success"

if __name__ == "__main__":
    print(main())

This is my docker container:

# syntax=docker/dockerfile:1

FROM openjdk:11

ENV PATH = "${PATH}:/chromedriver/chromedriver.exe"


RUN apt-get update && apt-get install -y \
      software-properties-common \
      unzip \
      curl \
      xvfb \
      wget \
          bzip2 \
          snapd

# Chrome
RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

# Python
RUN apt-get update && apt-get install -y \
    python2.7 \
    python-setuptools \
    python3-pip

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

CMD python3 test.py

When I first started my project, I attempted to do it with firefox but due to certain limitations chose to switch to chrome.

After trying to do research, there were suggestions to pass the path of chromedriver to the service object and add the path of chromedriver to the PATH in the docker container, both of which I have already done as shown above. I continue to get the exact same error.

I haven't been able to find any other solutions to the above issue so I would greatly appreciate any help!


Solution

  • In case anyone else stumbles across this and has a similar problem, this is how I solved it.

    I simply removed the service object entirely. It seems that for whatever reason, the service object wasn't configured correctly or even needed once I had added the ChromeDriver path to my System Path on the dockerfile. The code snippet now reads like this:

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")
    
    driver = webdriver.Chrome(options=chrome_options)