使用docker compose - jenhaoyang/backend_blog GitHub Wiki

# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: "3"
services:
  selenium-hub:
    image: selenium/hub:3.12.0-boron
    container_name: selenium-hub
    ports:
      - "4448:4444"
  chrome:
    image: selenium/node-chrome:3.12.0-boron
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  firefox:
    image: selenium/node-firefox:3.12.0-boron
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444

測試

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(
    command_executor='http://selenium-hub:4448/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME,
)

browser.get('http://www.google.com')
print(browser.title)
# Google

browser.save_screenshot("chrome.png")

參考:
https://maliaotw.github.io/2018/06/08/Docker%20Selenium%281%29%20%E6%90%AD%E5%BB%BA/

⚠️ **GitHub.com Fallback** ⚠️