selenium 遥控装卸车

ar7v8xwq  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(105)

here相同。docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=my_app
    ports:
      - '5432:5432'
  chrome:
    image: selenium/standalone-chrome
    hostname: chrome
    privileged: true
    shm_size: 2g
  web:
    build: .
    image: my-app
    ports:
      - "8000:8000"
    depends_on:
      - db
    command: sh -c "python manage.py migrate && 
             python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    environment:
      - DB_USER=postgres
      - DB_PASSWORD=postgres
      - DB_HOST=db
      - DB_NAME=my_app

一切按预期开始

% docker compose build && docker compose up
[+] Building 3.4s (11/11) FINISHED                                              
 => [internal] load build definition from Dockerfile                       0.1s
 => => transferring dockerfile: 189B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 34B                                           0.0s
 => [internal] load metadata for docker.io/library/python:3.11.1-bullseye  3.1s
 => [auth] library/python:pull token for registry-1.docker.io              0.0s
 => [internal] load build context                                          0.1s
 => => transferring context: 8.58kB                                        0.0s
 => [1/5] FROM docker.io/library/python:3.11.1-bullseye@sha256:cc4910af48  0.0s
 => CACHED [2/5] COPY requirements.txt requirements.txt                    0.0s
 => CACHED [3/5] RUN pip install -r requirements.txt                       0.0s
 => CACHED [4/5] COPY . /app                                               0.0s
 => CACHED [5/5] WORKDIR /app                                              0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:10e58eeb73e4651e1e1aedb921fdde3b389cadc204787  0.0s
 => => naming to docker.io/library/my-app                    0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/3
 ⠿ Container my_app-db-1      Created                        0.0s
 ⠿ Container my_app-chrome-1  Created                        0.0s
 ⠿ Container my_app-web-1     Recreated                      0.2s
Attaching to my_app-chrome-1, my_app-db-1, my_app-web-1
my_app-db-1      | 
my_app-db-1      | PostgreSQL Database directory appears to contain a database; Skipping initialization
my_app-db-1      | 
my_app-db-1      | 2023-01-08 14:01:28.671 UTC [1] LOG:  starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
my_app-db-1      | 2023-01-08 14:01:28.672 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
my_app-db-1      | 2023-01-08 14:01:28.673 UTC [1] LOG:  listening on IPv6 address "::", port 5432
my_app-db-1      | 2023-01-08 14:01:28.688 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_app-db-1      | 2023-01-08 14:01:28.699 UTC [28] LOG:  database system was shut down at 2023-01-08 13:54:14 UTC
my_app-db-1      | 2023-01-08 14:01:28.720 UTC [1] LOG:  database system is ready to accept connections
my_app-chrome-1  | 2023-01-08 14:01:28,791 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
my_app-chrome-1  | 2023-01-08 14:01:28,803 INFO RPC interface 'supervisor' initialized
my_app-chrome-1  | 2023-01-08 14:01:28,803 CRIT Server 'unix_http_server' running without any HTTP authentication checking
my_app-chrome-1  | 2023-01-08 14:01:28,808 INFO supervisord started with pid 8
my_app-chrome-1  | 2023-01-08 14:01:29,811 INFO spawned: 'xvfb' with pid 10
my_app-chrome-1  | 2023-01-08 14:01:29,819 INFO spawned: 'vnc' with pid 11
my_app-chrome-1  | 2023-01-08 14:01:29,833 INFO spawned: 'novnc' with pid 12
my_app-chrome-1  | 2023-01-08 14:01:29,849 INFO spawned: 'selenium-standalone' with pid 14
my_app-chrome-1  | Setting up SE_NODE_GRID_URL...
my_app-chrome-1  | 2023-01-08 14:01:29,911 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
my_app-chrome-1  | 2023-01-08 14:01:29,913 INFO success: vnc entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
my_app-chrome-1  | 2023-01-08 14:01:29,913 INFO success: novnc entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
my_app-chrome-1  | 2023-01-08 14:01:29,914 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
my_app-chrome-1  | Selenium Grid Standalone configuration: 
my_app-chrome-1  | [network]
my_app-chrome-1  | relax-checks = true
my_app-chrome-1  | 
my_app-chrome-1  | [node]
my_app-chrome-1  | session-timeout = "300"
my_app-chrome-1  | override-max-sessions = false
my_app-chrome-1  | detect-drivers = false
my_app-chrome-1  | drain-after-session-count = 0
my_app-chrome-1  | max-sessions = 1
my_app-chrome-1  | 
my_app-chrome-1  | [[node.driver-configuration]]
my_app-chrome-1  | display-name = "chrome"
my_app-chrome-1  | stereotype = '{"browserName": "chrome", "browserVersion": "108.0", "platformName": "Linux"}'
my_app-chrome-1  | max-sessions = 1
my_app-chrome-1  | 
my_app-chrome-1  | Starting Selenium Grid Standalone...
my_app-chrome-1  | Tracing is disabled
my_app-web-1     | Operations to perform:
my_app-web-1     |   Apply all migrations: admin, auth, contenttypes, core, sessions
my_app-web-1     | Running migrations:
my_app-web-1     |   No migrations to apply.
my_app-web-1     | Watching for file changes with StatReloader
my_app-web-1     | Performing system checks...
my_app-web-1     | 
my_app-web-1     | System check identified no issues (0 silenced).
my_app-web-1     | January 08, 2023 - 14:01:33
my_app-web-1     | Django version 4.1.5, using settings 'my_app.settings'
my_app-web-1     | Starting development server at http://0.0.0.0:8000/
my_app-web-1     | Quit the server with CONTROL-C.
my_app-chrome-1  | 14:01:33.430 INFO [LoggingOptions.configureLogEncoding] - Using the system default encoding
my_app-chrome-1  | 14:01:33.453 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing
my_app-chrome-1  | 14:01:35.487 INFO [NodeOptions.getSessionFactories] - Detected 2 available processors
my_app-chrome-1  | 14:01:35.608 INFO [NodeOptions.report] - Adding chrome for {"browserVersion": "108.0","se:noVncPort": 7900,"browserName": "chrome","platformName": "LINUX","se:vncEnabled": true} 1 times
my_app-chrome-1  | 14:01:35.649 INFO [Node.<init>] - Binding additional locator mechanisms: name, relative, id
my_app-chrome-1  | 14:01:35.709 INFO [GridModel.setAvailability] - Switching Node 9f76899f-6574-4e21-9413-d6141aa6c584 (uri: http://172.25.0.2:4444) from DOWN to UP
my_app-chrome-1  | 14:01:35.709 INFO [LocalDistributor.add] - Added node 9f76899f-6574-4e21-9413-d6141aa6c584 at http://172.25.0.2:4444. Health check every 120s
my_app-chrome-1  | 14:01:36.147 INFO [Standalone.execute] - Started Selenium Standalone 4.7.2 (revision 4d4020c3b7): http://172.25.0.2:4444

日志显示http://172.25.0.2:4444上当前有 selenium 可用,因此我尝试:

>>> from selenium.webdriver import ChromeOptions, Remote
>>> options = ChromeOptions()
>>> options.add_argument('--headless')
>>> driver = Remote('http://172.25.0.2:4444')

它会一直挂起,没有特殊的输出/日志消息/任何进一步的事情发生,它只是一直挂起,直到^c.那么到底应该如何使用它呢?同样出于某种原因,如果ip地址被替换为http://chrome:4444,连接将被拒绝。

lztngnrs

lztngnrs1#

docker命令将使用selenium/standalone-chrome:87.0拉取Chrome 87.0版本,如下所示,下载完镜像后,运行docker images命令检查下载是否成功。

相关问题