簡體   English   中英

如何使用 Selenium 在 Docker 容器中運行 Chrome Headless?

[英]How to run Chrome Headless in Docker Container with Selenium?

我正在嘗試運行一個簡單的測試文件,該文件旨在在 openjdk docker 容器內的 chrome 上打開 google.com 並在完成后返回“完全成功”,但是,我不斷收到相同的錯誤消息,指出“服務 object 沒有屬性過程”。 這是我不斷收到的錯誤:

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'

這是我正在運行的代碼:

#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())

這是我的 docker 容器:

# 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

當我第一次開始我的項目時,我嘗試用 firefox 來做,但由於某些限制選擇切換到 chrome。

嘗試研究后,有建議將chromedriver的路徑傳遞給服務object,並將chromedriver的路徑添加到docker容器中的PATH中,這兩個我都已經做了,如上所示。 我繼續得到完全相同的錯誤。

我還沒有找到上述問題的任何其他解決方案,所以我將不勝感激任何幫助!

萬一其他人偶然發現這個問題並遇到類似的問題,這就是我解決它的方法。

我只是完全刪除了服務 object。 似乎無論出於何種原因,服務 object 都未正確配置,甚至在我將 ChromeDriver 路徑添加到 dockerfile 上的系統路徑后甚至不需要。代碼片段現在如下所示:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM